What is the best way to mock asynchronous (#Async) method with mockito? Provided service below:
#Service
#Transactional(readOnly=true)
public class TaskService {
#Async
#Transactional(readOnly = false)
public void createTask(TaskResource taskResource, UUID linkId) {
// do some heavy task
}
}
Mockito's verification as below:
#RunWith(SpringRunner.class)
#WebMvcTest(SomeController.class)
public class SomeControllerTest {
#Autowired
MockMvc mockMvc;
#MockBean
private TaskService taskService;
#Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
// other details omitted...
#Test
public void shouldVerify() {
// use mockmvc to fire to some controller which in turn call taskService.createTask
// .... details omitted
verify(taskService, times(1)) // taskService is mocked object
.createTask(any(TaskResource.class), any(UUID.class));
}
}
The test method shouldVerify above will always throw:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
-> at SomeTest.java:77) // details omitted
-> at SomeTest.java:77) // details omitted
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.
The exception above won't happend if I remove #Async from the TaskService.createTask method.
Spring Boot version: 1.4.0.RELEASE
Mockito version: 1.10.19
Found out that by changing the Async mode to AspectJ fixed the issue:
#EnableCaching
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan(lazyInit = true)
#EnableAsync(mode = AdviceMode.ASPECTJ) // Changes here!!!
public class Main {
public static void main(String[] args) {
new SpringApplicationBuilder().sources(Main.class)
.run(args);
}
}
I'll accept this as a temporary hackish solution until I understand what's the real root cause of this issue.
There's a bug in Spring Boot that we hope to fix in 1.4.1. The problem is that your mock TaskService is still being called asynchronously which breaks Mockito.
You could work around the problem by creating an interface for TaskService and creating a mock of that. As long as you leave the #Async annotation only on the implementation things will then work.
Something like this:
public interface TaskService {
void createTask(TaskResource taskResource, UUID linkId);
}
#Service
#Transactional(readOnly=true)
public class AsyncTaskService implements TaskService {
#Async
#Transactional(readOnly = false)
#Override
public void createTask(TaskResource taskResource, UUID linkId) {
// do some heavy task
}
}
Related
I'm using Mockito 3.4.6 in unit test, actually, i have integrated Mockito to my unit test and it works well. While, now i need to optimize some unit test, it's a special dependency injection that the injected object doesn't have no-arg constructor, I tried #Spy but it didn't work.
My Test: I tried 1. #Spy; 2. #Spy with setting instance using = getDtInsightApi(); 3. #Spy with #InjectMocks, all of tests are failed. As Mockito docs said, seems it can't work for this case.
#InjectMocks Mockito will try to inject mocks only either by constructor injection,
setter injection, or property injection in order and as described below.
Also if only use #Spy, it will throw MockitoException:
org.mockito.exceptions.base.MockitoException:
Failed to release mocks
This should not happen unless you are using a third-part mock maker
...
Caused by: org.mockito.exceptions.base.MockitoException: Unable to initialize #Spy annotated field 'api'.
Please ensure that the type 'DtInsightApi' has a no-arg constructor.
...
Caused by: org.mockito.exceptions.base.MockitoException: Please ensure that the type 'DtInsightApi' has a no-arg constructor.
See my pseudocode as below:
configure class:
#Configuration
public class SdkConfig {
#Resource
private EnvironmentContext environmentContext;
#Bean(name = "api")
public DtInsightApi getApi() {
DtInsightApi.ApiBuilder builder = new DtInsightApi.ApiBuilder()
.setServerUrls("sdkUrls")
return builder.buildApi();
}
}
DtInsightApi class with no public no-arg constructor and get instance by its inner class
public class DtInsightApi {
private String[] serverUrls;
DtInsightApi(String[] serverUrls) {
this.serverUrls = serverUrls;
}
// inner class
public static class ApiBuilder {
String[] serverUrls;
public ApiBuilder() {
}
...code...
public DtInsightApi buildApi() {
return new DtInsightApi(this.serverUrls);
}
}
...code...
}
unit test class:
public Test{
#Autowired
private PendingTestService service;
#Spy
private Api api = getDtInsightApi();
#Mock
private MockService mockService;
#Before
public void setUp() throws Exception {
// open mock
MockitoAnnotations.openMocks(this);
// i use doReturn(...).when() for #Spy object
Mockito.doReturn(mockService).when(api)
.getSlbApiClient(MockService.class);
Mockito.when(mockService.addOrUpdate(any(MockDTO.class)))
.thenReturn(BaseObject.getApiResponseWithSuccess());
}
public DtInsightApi getDtInsightApi () {
return new DtInsightApi.ApiBuilder()
.setServerUrls(new String[]{"localhost:8080"})
.buildApi();
}
#Test
public void testUpdate() {
service.update();
}
}
PendingTestService:
#Service
public class PendingTestService{
#Autowired
DtInsightApi api;
public void update() {
// here mockService isn't the object i mocked
MockService mockService = api.getSlbApiClient(MockService.class);
mockService.update();
}
}
Question: How to mock the DI object DtInsightApi which doesn't have no-arg constructor.
After checked Spring docs about unit test, I found a solution using #MockBean.
Spirng docs:https://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/html/boot-features-testing.html
According to Spring docs, you can use #MockBean to mock a bean inside your ApplicationContext, so i can use #MockBean to mock DtInsightApi.
It’s sometimes necessary to mock certain components within your application context when running tests. For example,
you may have a facade over some remote service that’s unavailable during development. Mocking can also be useful when
you want to simulate failures that might be hard to trigger in a real environment.
Spring Boot includes a #MockBean annotation that can be used to define a Mockito mock for a bean inside your ApplicationContext.
You can use the annotation to add new beans, or replace a single existing bean definition. The annotation can be used directly on test classes,
on fields within your test, or on #Configuration classes and fields. When used on a field, the instance of the created mock will also be injected.
Mock beans are automatically reset after each test method.
My Solution: Use #MockBean and BDDMockito.given(...).willReturn(...), use
#Qualifier("api") to specify the bean name because #MockBean injected by class type, if you have same class beans, you need to specify bean name.
My code in test class:
public class Test{
#MockBean
#Qualifier("api")
private DtInsightApi api;
#Mock
private MockService mockService;
#Before
public void setUp() throws Exception {
// open mock
MockitoAnnotations.openMocks(this);
BDDMockito.given(this.api.getSlbApiClient(MockService.class)).willReturn(mockService);
}
#Autowired
private PendingTestService service;
#Test
public void testUpdate() {
service.update();
}
}
Debug the mockService you can see mockService instance is generated by Mockito, mock succeed.
You can also refer to Spring docs example: mock RemoteService in unit test.
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.context.*;
import org.springframework.boot.test.mock.mockito.*;
import org.springframework.test.context.junit4.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;
#RunWith(SpringRunner.class)
#SpringBootTest
public class MyTests {
#MockBean
private RemoteService remoteService;
#Autowired
private Reverser reverser;
#Test
public void exampleTest() {
// RemoteService has been injected into the reverser bean
given(this.remoteService.someCall()).willReturn("mock");
String reverse = reverser.reverseSomeCall();
assertThat(reverse).isEqualTo("kcom");
}
}
I have a service in my Spring Boot Project in which i have method which takes an interface.
interface IT {}
class AService {
public String method(IT it) {}
}
I have two classes which implements that interface.
class AIT implements IT {}
class BIT implements IT {}
I am using this service method in some other service passing the AIT/BIT class object according to my need.
Now, I am writing the test cases for other service mocking the Service
public class OtherServiceTests {
#MockBean
private Service service;
#Before
public void setUp() {
// none of these mocks working
Mockito.when(service.method(Mockito.any()))
.thenReturn("");
Mockito.when(service.method(Mockito.any(IT.class)))
.thenReturn("");
Mockito.when(service.method(Mockito.any(BIT.class)))
.thenReturn("");
Mockito.when(service.method(Mockito.any(AIT.class)))
.thenReturn("");
// all returing to NullPointerException
otherService = new OtherSerice();
}
}
None of these mocks are working for this method only. Other mocks are working fine. It is returning NullPointerException which makes the tests fail.
I am new to testing using mockito. If anyone can guide me for this solution than this will be very helpful for me.
Mock is not initialized and the annotation #MockBean should be replaced with #Mock.
Try to change it like this:
public class AServiceTest {
#Mock
AService service;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
#Test
public void myTest(){
Mockito.when(service.method(Mockito.any())).thenReturn("");
assertEquals("", service.method(new AIT()));
}
}
I have a simple service interface:
#Service
#Validated
public interface UserService
{
void createUser (#Email String userEmail);
void refreshUsers ();
}
I want to test 2 things on UserService:
The validation annotation #Email is working.
The calls on method refreshUsers() can be verified by verify().
Therefore, I created the following test case with SpringJUnit4ClassRunner:
#RunWith(value = SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = UserServiceTest.TestConfig.class)
public class UserServiceTest
{
#Configuration
public static class TestConfig
{
#Bean
public UserService userService ()
{
return mock(UserService.class);
}
#Bean
public MethodValidationPostProcessor methodValidationPostProcessor ()
{
return new MethodValidationPostProcessor();
}
}
#Autowired
private UserService userService;
#Test
public void createUserWithValidEmail ()
{
userService.createUser("aaa#example.com");
}
#Test
public void createUserWithInvalidEmail ()
{
assertThatThrownBy(() -> userService.createUser("aaa"))
.isInstanceOf(ConstraintViolationException.class);
}
#Test
public void refreshUsersCalled1 ()
{
userService.refreshUsers();
verify(userService).refreshUsers();
}
#Test
public void refreshUsersCalled2 ()
{
userService.refreshUsers();
verify(userService).refreshUsers();
}
The test case refreshUsersCalled2() will fail with the following message:
Missing method call for verify(mock) here:
-> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Example of correct verification:
verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
However, the method refreshUsers() is not one of final/private/equals()/hashCode() methods.
After some investigation, I found that the problem came from the conflict between mock() and MethodValidationPostProcessor.
If I remove the MethodValidationPostProcessor Spring bean from TestConfig, the above issues will be solved. However, the test case createUserWithInvalidEmail will fail because the validation mechanism is gone.
Is there any way to test both validation and verify() in the same test?
The full example is at https://github.com/johnlinp/misc-demo/tree/master/mockito-with-validation-in-spring-test.
I want to test a service level method that is cached by the #Cacheable annotation. I am mocking the service using Mockito. Below is my cache config and actual test
The cache has not been used and the Mockito verify fails with the method being called twice (instead of once)
My cache config:
#Configuration
#EnableCaching
public class CacheConfiguration implements CachingConfigurer {
private static final Log LOG = LogFactory.getLog(CacheConfiguration.class);
#Override
#Bean
public CaffeineCacheManager cacheManager() {
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(
"sample-cache");
caffeineCacheManager.setCaffeine(caffeineCacheBuilder());
caffeineCacheManager.setAllowNullValues(false);
return caffeineCacheManager;
}
Caffeine<Object, Object> caffeineCacheBuilder() {
return Caffeine.newBuilder().maximumSize(50)
.expireAfterAccess(30, TimeUnit.MINUTES).softValues();
}
Test:
#RunWith(SpringRunner.class)
#SpringBootTest(classes = *Application.class)
#AutoConfigureMockMvc
public class CachingIntegrationTest {
#MockBean
private Service service;
#Before
public void setUp() throws {
Mockito.reset(service);
String eg = "eg"''
Mockito.when(service.serviceMethod(argument))
.thenReturn(eg);
}
#Test
public void verifyCache() throws {
service.serviceMethod(argument);
service.serviceMethod(argument);
Mockito.verify(service, Mockito.times(1)).serviceMethod(argument);
}
I don't know for sure, but I'd be surprised if #Cacheable annotations still work even if don't actually use the annotated object but instead a Mockito mock of it. The reason is that both techniques (i.e. the aspect-oriented programming annotation #Cacheable and mocking) are implemented with Java dynamic proxies (or equivalent bytecode generation), so they are likely to get in each other's way.
What you should do instead is mock the collaborators of your service, and then check the number of invocations on these.
I have a Spring-boot project, in witch I have controller, service and mapper layer. Now I want to test a service and I want to mock the mapper. I do it in this way:
Test:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(Application.class)
#Transactional
public class SomeServiceTest extends AbstractTransactionalJUnit4SpringContextTests {
#Mock
private AMapper aMapper;
#Autowired
#InjectMocks
AService aService;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
executeSqlScript("classpath:insertSomeData.sql", false);
}
#Test
public void testMethod() throws Exception {
//prepareSomeData
aService.callMethod(someData);
verify(aMapper).callTheRightMethod(rightObject);
}
And the service:
#Service
#Transactional(readOnly = true)
public class AServiceImpl implements AService {
#Autowired
BMapper bMapper;
#Autowired
CMapper cMapper;
#Override
#Transactional(readOnly = false)
public SomeReturnObject callMethod(SomeData someData)throws Exception {
//some execution to obtain aResult
if(true){
aMapper.callTheRightMethod(aResult);}
else
aMapper.callWrongMethod(aResult);
}
Now when I execute the test the result is:
Wanted but not invoked:
aMapper.callTheRightMethod{..}
Actually, there were zero interactions with this mock.
When i debug then I see that the method is called, but probably it's the wrong mapper (not the mocked). Have you some tips to figure out that issue?
I can't see the mock interaction recording here. It should come before the actual invocation. It should be something like this.
Mockito.when(aMapper.callTheRightMethod(Mockito.any()).thenReturn(rightObject);
The flow should be like this. Firstly record the mocks, then perform actual invocation and finally verify the mock interactions. As above #Autowire is not needed for the test class. Please remove that too. Instead create a new instance of service class by passing some data through it's constructor. Hope this helps. Happy coding !
I don't exactly understand why would you start up spring context for testing just a service layer. Test only one layer at a time.
That's how I would address the problem. (If something does not compile, my apologies..writing from top of my head)
#RunWith(MockitoJUnit4ClassRunner.class)
public class SomeServiceTest {
#Mock
private AMapper aMapper;
#InjectMocks
AService aService = new AService();
#Test
public void testMethod() throws Exception {
// given
Mockito.doReturn(aResult).when(aMapper).getAResult();
// when
aService.callMethod(someData);
// then
verify(aMapper).callTheRightMethod(rightObject);
}