What is the difference between @ExtendWith(SpringExtension.class) and @ExtendWith(MockitoExtension.class)?

When involving Spring: If you want to use Spring test framework features in your tests like for example @MockBean, then you have to use @ExtendWith(SpringExtension.class). It replaces the deprecated JUnit4 @RunWith(SpringJUnit4ClassRunner.class) When NOT involving Spring: If you just want to involve Mockito and don’t have to involve Spring, for example, when you just want to … Read more

Using Mockito, how do I intercept a callback object on a void method?

For functions returning void, use doAnswer() doAnswer(…).when(mockedObject).handle(any(Callback[].class)); And an Answer that performs the interception must go in as the parameter to doAnswer, e.g. as an anonymous class: new Answer() { public Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Mock mock = invocation.getMock(); return null; }} In this case args will be the array Callback[]!

Bad state: Mock method was not called within `when()`. Was a real method called?

This issue may happen when you implement a method you want to mock instead of letting Mockito do that. This code below will return Bad state: Mock method was not called within when(). Was a real method called?: class MockFirebaseAuth extends Mock implements FirebaseAuth { FirebaseUser _currentUser; MockFirebaseAuth(this._currentUser); // This method causes the issue. Future<FirebaseUser> … Read more

How to inject multiple mocks of the same interface

It should be enough to name your mocks serviceA and serviceB. From Mockito documentation: Property setter injection; mocks will first be resolved by type, then, if there is several property of the same type, by the match of the property name and the mock name. In your example: @InjectMocks ServiceCaller classUnderTest; @Mock SomeService serviceA; @Mock … Read more

How to mock just one static method in a class using Mockito?

By default all methods are mocked. However, using Mockito.CALLS_REAL_METHODS you can configure the mock to actually trigger the real methods excluding only one. For example given the class Sample: class Sample{ static String method1(String s) { return s; } static String method2(String s) { return s; } } If we want to mock only method1: … Read more

Attempt to mockito mock any class generates ExceptionInInitializerError

I received this error when I was missing the two dexmaker dependencies. Adding these lines to the app/gradle.build file is working for me. androidTestCompile ‘org.mockito:mockito-core:1.10.19’ androidTestCompile ‘com.google.dexmaker:dexmaker:1.2’ androidTestCompile ‘com.google.dexmaker:dexmaker-mockito:1.2’ I am also using Android Studio and have found it to be a good idea to restart AS after altering the dependencies.

How to use @InjectMocks and initMocks() with an object that has a required String parameter?

I think the simple answer is not to use @InjectMocks, and instead to initialise your object directly. The only downside I can see is that you’re not testing the injection, but then with @InjectMocks, I think you’d be testing it with Mockito’s injection implementation, rather than your real framework’s implementation anyway, so no real difference. … Read more

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