Testing in Android Flashcards

1
Q

Why Unit Tests?

A
  • are good documentation
  • reduce bugs in new and existing features
  • allow refactoring (no fear of breaking existing code-> so you can constantly improve the program)
  • reduce fear
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Categories of tests in Android

A

Local Unit Tests

Instrumented Test

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Local Unit Tests

A
  • Run on your machine’s local JVM
  • Used when :
    • No Android Framework dependecies
    • You can mock the android dependecies
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Intrumented Tests

A
  • Run on a hardware edvice or emulator
  • Give you acces to info such as the Context
  • Used when you write Integration and Functional UI tests to automate User Interaction.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a Unit Test?

A

A unit test verifies in isolation the functionality of a certain component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Mocking dependecies

A

It might happen that a class eg.Presenter doesn’t stand in isolation- it also requires a View that updates UI and a Repository, where data is stored.

We dont care about the concrete impleemntation of the View and Repository, at this point, we only want to make sure that Presenter behaves as expected.

So, instead of relying on actual implementations of these dependecies we can use Mock Objects to test our class.

Mocking is creating objects that simulate the behaviour of real objects

One popular framework for mocking on Android is MOCKITO.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Mockito

A

Mockito uses RUNTIME CODE GENERATION techniques to build implementations of mocks.

For ex:
ArrayList mock = mock(ArrayList.class)

Mockito is building a new generatoed class that extends ArrayList, but has 'mock' implementation that can be configured by you. 
When your Java process running the test exits, the generated types vanish. They live, only at runtime.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Test doubles

A

Test double is a generic term used for indicating some kind of dependency substitution for the test purposes
Test doubles are:
- STUB uses STATE verification while
- MOCK uses BEHAVIOR verification.
- SPY is a STUB, that use BEHAVIOR verification

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Difference between Mock and Stub

A

WHAT IS and WHEN TO USE MOCK?

  • A mock is a simulation of an object, used to replacing a dependency, they are programmed with expectations.
  • Used to verify object behaviour during a test (ex. if are called corect methods)

WHAT IS and WHEN TO USE STUB?

  • Stub is a hand-written class that mimic the dependency’s behavior, but do that with significant shortcuts, to help with testing.
  • You can use them to replace problematic pieces of code (for ex. 1. stub a database by implemented a simple structure for storing some data, SO we no longer need an actual database for our test 2. avoid making actual requests to a server from tests);

WHEN TO USE SPY?

  • Spy object is a partial mock, diferece is that a spy guaratess that real methods are called.
  • Used to track all interactions with it (many times a function was called, what arguments were passed)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Difference between Mock and Spy

A
  • When Mockito creates a mock – it does so from the Class of an Type, not from an actual instance. The mock simply creates a bare-bones shell instance of the Class, entirely instrumented to track interactions with it.
  • The Spy will wrap an existing instance. It will still behave in the same way as the normal instance – the only difference is that it will also be instrumented to track all the interactions with it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Example

A

@Mock
Dependecy mockDep;
ObjUnderTest objUnderTest;

@Before
init(){
    objUnderTest = new ObjUnderTest(); // real instance
    MockitoAnnotations.initMocks(this);
}
@Test
testSomething(){   
    objUnderTest.testSomething();
    verify(mockDep).methodToBeCalled();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

UI Testing Android

A

UI Automator - allows many system stuff, as example turn on/off wifi, notification, access to any application

Espresso -allows you test many things inside your application, simulating user interactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Steps to manually test UI with Espresso

A
  1. Match the view (ViewMatcher offers withId, withText ..)
  2. Act on the view (ViewAction offers click, doubleclick..)
  3. Check if what happened matches what we expected
    (ViewAssertion matches, isDisplay(), isBelow())

onView(ViewMatcher)
.perform(ViewAction)
.check(ViewAssertion);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly