JUnit Mockito
JUnit Mockito
Statement Description
fail(String) Let the method fail. Might be used to check
that a certain part of the code is not reached.
Or to have a failing test before the test code is
implemented. The String parameter is
optional.
assertTrue([message], Checks that the boolean condition is true.
boolean condition)
Statement Description
assertEquals([String Test that float or double values match.
message], expected, The tolerance is the number of decimals which
actual, tolerance) must be the same.
assertNull([message], Checks that the object is null.
object)
Note
You should provide meaningful messages in
assertions so that it is easier for the developer
to identify the problem.
import org.junit.*;
import static org.junit.Assert.*;
@Test
public void testIsPositive() {
assertTrue(Arithmetic.isPositive(5));
assertFalse(Arithmetic.isPositive(-5));
assertFalse(Arithmetic.isPositive(0));
}
}
Writing a JUnit test class
Limitations
Spy
Mockito
Limitations
Spy
Mockito for mocking objects
Limitations
Spy
Using Mockito
Limitations
Spy
Limitations
Limitations
Spy
Configuring the mock objects
doReturn(object).when(kdskfsk).methodCall
works similar.
Configuring the mock objects
@Test
public void test1 ( ) {
MyClass test =
Mockito.mock(MyClass.class);
test.when(test.getUniqueId( )).thenReturn(43)
;
Limitations
Spy
Verify the calls on the mock
objects
@Test
public void test1 ( ) {
MyClass test = Mockito.mock(MyClass.class);
test.when(test.getUniqueId( )).thenReturn(43);
Note
This kind of testing is sometimes
called behavior testing, because it does not
check the result of a method call, but it checks
that a method is called with the right parameters.
Mockito
Limitations
Spy
Spy
@Spy or the spy() method can be used to wrap a
real object. Every call, unless specified otherwise, is
delegated to the object.
// Lets mock a LinkedList
List list = new LinkedList( );
List spy = spy(list);
@Test
public void shouldDoSomething( ) {
MokitoAnnotations. initMocks(this);
verify(database).
Q/A
Thank you!