Unit Test1
Unit Test1
<unitOfWork>_<stateUnderTest>_<expectedBehavior>
isOverlap_interval1BeforeInterval2_falseReturned
@Nullable - implicitly tells that my function can be nullable in argument or return value
No need for a null case test in most cases.
Test Double
1. Fake -Functional substitution for real units (optimized for tests) - fake -optimized for tests.
2. Stubs - returns predefined data (like API results) - predefined data, like network data
3. Mock (record interaction during the test) - record interaction with itself
When creating fake test double classes under Test class make sure it ends with ‘Td’
If the test is not passed, there will be mainly 3 reasons for that
● There is an issue in the test code.
● Bug in production code.
● A wrong requirement is given.
You used Test Doubles, the classes(LoginHttpEndpointSyncTd) you use in the test class to substitute external
dependencies. But this is only using 30% of java developers others using mockito frameworks.
--------------------------------------------------------------------------------------------------------------------------------------
Mockito
Verify() -> Mockito assertion asserts that some method is called no. of times.
Eg: mLoginHttpEndpointSyncMock should call loginSync() exactly one time and capture argument.
mLoginHttpEndpointSyncMock, times(1)).loginSync(ac.capture(), ac.capture());
verify(
doAnswer() ->
doAnswer(new A nswer() {
@Override
public O bject answer(InvocationOnMock invocation) throws Throwable {
bject[
O ] args = invocation.
getArguments();
C allback callback = (Callback) a rgs[1];
c allback.onGetCartItemsFailed(G etCartItemsHttpEndpoint.FailReason. NETWORK_ERROR)
;
r eturn null;
}
}).when(m GetCartItemsHttpEndpointMock) .getCartItems(anyInt(), any(Callback.class) );
when() -> Action when some method is called with some value
when(mLoginHttpEndpointSyncMock.loginSync(any(
String.class), any(String.class)))
.thenReturn(new
LoginHttpEndpointSync.EndpointResult(LoginHttpEndpointSync.EndpointResultStatus.SUCCESS, AUTH_TOKEN)
);
doThrow() -> Exception thws when some method is called with some value
doThrow(new NetworkErrorException())
String.class), any(S
.when(mLoginHttpEndpointSyncMock).loginSync(any( tring.class));
--------------------------------------------------------------------------------------------------------------------------------------
Object Vs Data Structures
Mature programmers know that the idea that everything is an object is a myth
--------------------------------------------------------------------------------------------------------------------------------------
Structure
--------------------------------------------------------------------------------------------------------------------------------------
Without Mockito Annotation
@Before
public void setup() throws Exception {
mLoginHttpEndpointSyncMock = mock(LoginHttpEndpointSync.class);
mAuthTokenCacheMock = mock( AuthTokenCache.class);
EventBusPoster.class);
mEventBusPosterMock = mock(
@RunWith(MockitoJUnitRunner.class)
public class LoginUseCaseSyncTest {
@Before
public void setup() throws Exception {
SUT = new LoginUseCaseSync(mLoginHttpEndpointSyncMock, mAuthTokenCacheMock, m
EventBusPosterMock);
success();
}
--------------------------------------------------------------------------------------------------------------------------------------
Mockito considerations
1. A matter of personal preference (Mockito / Test doubles)
2. More Mockito more complexity
3. Not trivially simple with Mockito then fallback to the test doubles.
import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class ${NAME} {
// region constants
// endregion constants
${CLASS_NAME} SUT;
@Before
public void setup() throws Exception {
SUT = new ${CLASS_NAME} ();
${BODY}
}
--------------------------------------------------------------------------------------------------------------------------------------