Difference between @Mock, @MockBean and Mockito.mock()

Plain Mockito library import org.mockito.Mock; … @Mock MyService myservice; and import org.mockito.Mockito; … MyService myservice = Mockito.mock(MyService.class); come from the Mockito library and are functionally equivalent. They allow to mock a class or an interface and to record and verify behaviors on it. The way using annotation is shorter, so preferable and often preferred. Note … Read more

Using Mockito to test abstract classes

The following suggestion let’s you test abstract classes without creating a “real” subclass – the Mock is the subclass. use Mockito.mock(My.class, Mockito.CALLS_REAL_METHODS), then mock any abstract methods that are invoked. Example: public abstract class My { public Result methodUnderTest() { … } protected abstract void methodIDontCareAbout(); } public class MyTest { @Test public void shouldFailOnNullIdentifiers() … Read more

Mockito: Inject real objects into private @Autowired fields

Use @Spy annotation @RunWith(MockitoJUnitRunner.class) public class DemoTest { @Spy private SomeService service = new RealServiceImpl(); @InjectMocks private Demo demo; /* … */ } Mockito will consider all fields having @Mock or @Spy annotation as potential candidates to be injected into the instance annotated with @InjectMocks annotation. In the above case ‘RealServiceImpl’ instance will get injected … Read more

Mockito verify order / sequence of method calls

InOrder helps you to do that. ServiceClassA firstMock = mock(ServiceClassA.class); ServiceClassB secondMock = mock(ServiceClassB.class); Mockito.doNothing().when(firstMock).methodOne(); Mockito.doNothing().when(secondMock).methodTwo(); //create inOrder object passing any mocks that need to be verified in order InOrder inOrder = inOrder(firstMock, secondMock); //following will make sure that firstMock was called before secondMock inOrder.verify(firstMock).methodOne(); inOrder.verify(secondMock).methodTwo();

How to tell a Mockito mock object to return something different the next time it is called?

You could also Stub Consecutive Calls (#10 in 2.8.9 api). In this case, you would use multiple thenReturn calls or one thenReturn call with multiple parameters (varargs). import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; public class TestClass { private Foo mockFoo; @Before public void setup() { setupFoo(); } @Test … Read more

Mockito – difference between doReturn() and when()

The two syntaxes for stubbing are roughly equivalent. However, you can always use doReturn/when for stubbing; but there are cases where you can’t use when/thenReturn. Stubbing void methods is one such. Others include use with Mockito spies, and stubbing the same method more than once. One thing that when/thenReturn gives you, that doReturn/when doesn’t, is … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)