Size: 2120
Comment:
|
← Revision 13 as of 2024-08-03 16:20:08 ⇥
Size: 2546
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
<<TableOfContents(2)>> |
|
Line 16: | Line 18: |
== methods purpose == | == Methods purpose == |
Line 20: | Line 22: |
* any(): matches anything * anyString(): matches any non-null String * eq(): argument that is equal to the given value. * doNothing().when(objx).objMethod() : void * doReturn(response).when(objx).objMethod() : return object * doThrow(Exception.class).when(objx).objMethod() : throw exception * doCallRealMethod(): call real method |
|
Line 29: | Line 38: |
import org.junit.Assert; | import static org.junit.Assert.assertEquals; |
Line 48: | Line 57: |
Assert.assertEquals(123, responsex.size()); | assertEquals(123, responsex.size()); |
Line 63: | Line 72: |
//given | //given , setup mocks |
Line 66: | Line 75: |
//when | //when , call code |
Line 69: | Line 78: |
//then | //then , check results |
Contents
Mockito
Unit tests mocks framework.
Annotations
- Constructor injection
- Property setter injection
- Field injection
- @Mock
- @Test
Methods purpose
- mock(): create a mock
- when(): specify how a mock should behave
- verify(): to check mocked/stubbed methods were called with given arguments
- any(): matches anything
- anyString(): matches any non-null String
- eq(): argument that is equal to the given value.
- doNothing().when(objx).objMethod() : void
- doReturn(response).when(objx).objMethod() : return object
- doThrow(Exception.class).when(objx).objMethod() : throw exception
- doCallRealMethod(): call real method
Example
1 import static org.mockito.Matchers.any;
2 import static org.mockito.Matchers.anyObject;
3 import static org.mockito.Matchers.eq;
4 import static org.mockito.Mockito.doCallRealMethod;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7 import static org.junit.Assert.assertEquals;
8 import org.junit.Test;
9 import org.springframework.mock.env.MockEnvironment;
10
11 public class TestX{
12 @Test
13 public void testXyz() {
14 MockEnvironment env = new MockEnvironment();
15 env.setProperty("intx", "1");
16 env.setProperty("intz", "10");
17 String rsp1="rsp1";
18 String rsp2="rsp2";
19 ClassX cx = mock(ClassX.class);
20 when(cx.getURL(anyObject(), anyObject(), eq("aaaa"))).thenReturn(rsp1).thenReturn(rsp2);
21 doCallRealMethod().when(cx).initialize(any(), any());
22 doCallRealMethod().when(cx).doStuff(any(), any());
23
24 cx.initialize(null, env);
25 List<String> responsex = cx.doStuff(null, "kkll"));
26 assertEquals(123, responsex.size());
27 }
28 }
BDDMockito
Behavior Driven Development style of writing tests uses //given //when //then comments as fundamental parts of your test methods.
1 import static org.mockito.BDDMockito.*;
2
3 Dummy dummy = mock(Dummy.class);
4 Foo foo = new Foo(dummy);
5
6 public void doStuff() throws Exception {
7 //given , setup mocks
8 given(dummy.getObj()).willReturn(new Obj());
9
10 //when , call code
11 PlentyStuff ps = shop.getObj();
12
13 //then , check results
14 assertThat(ps, containObj());
15 }