如何在Spring Boot中使用Mockito模拟异步(@Async)方法



用mockito模拟异步(@Async)方法的最佳方法是什么?提供以下服务:

@Service
@Transactional(readOnly=true)
public class TaskService {
    @Async
    @Transactional(readOnly = false)
    public void createTask(TaskResource taskResource, UUID linkId) {
        // do some heavy task
    }
}

Mockito的验证如下:

@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));
    } 
}

上面的测试方法shouldVerify总是抛出:

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.

如果我从TaskService.createTask方法中移除@Async,上面的异常将不会发生。

Spring Boot version: 1.4.0.RELEASE

Mockito version: 1.10.19

发现通过将Async模式更改为AspectJ修复了这个问题:

@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);
    }
}

在我弄清楚问题的根本原因之前,我将把这作为一个临时的解决方案。

在Spring Boot中有一个bug,我们希望在1.4.1中修复。问题是您的mock TaskService仍然被异步调用,这会破坏Mockito。

您可以通过为TaskService创建一个接口并创建一个模拟来解决这个问题。只要你把@Async注释只留在实现上,事情就会起作用。

像这样:

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
    }
}

相关内容

  • 没有找到相关文章

最新更新