<> = Mockito = Unit tests mocks framework. * https://site.mockito.org/ * https://github.com/mockito/mockito * https://www.tutorialspoint.com/mockito/mockito_quick_guide.htm == Annotations == * @InjectMocks * 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 == {{{#!highlight java import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.springframework.mock.env.MockEnvironment; public class TestX{ @Test public void testXyz() { MockEnvironment env = new MockEnvironment(); env.setProperty("intx", "1"); env.setProperty("intz", "10"); String rsp1="rsp1"; String rsp2="rsp2"; ClassX cx = mock(ClassX.class); when(cx.getURL(anyObject(), anyObject(), eq("aaaa"))).thenReturn(rsp1).thenReturn(rsp2); doCallRealMethod().when(cx).initialize(any(), any()); doCallRealMethod().when(cx).doStuff(any(), any()); cx.initialize(null, env); List responsex = cx.doStuff(null, "kkll")); assertEquals(123, responsex.size()); } } }}} == BDDMockito == Behavior Driven Development style of writing tests uses //given //when //then comments as fundamental parts of your test methods. * https://en.wikipedia.org/wiki/Behavior-driven_development {{{#!highlight java import static org.mockito.BDDMockito.*; Dummy dummy = mock(Dummy.class); Foo foo = new Foo(dummy); public void doStuff() throws Exception { //given , setup mocks given(dummy.getObj()).willReturn(new Obj()); //when , call code PlentyStuff ps = shop.getObj(); //then , check results assertThat(ps, containObj()); } }}}