@Async和Mockito问题

时间:2015-06-22 19:58:24

标签: java spring unit-testing asynchronous mockito

我有这门课。

@Service
public class ConcurrentService{

    public Map<String, Object> createList(){
       this.asynCall();
    }

    @Async("taskExecutor")
    private Future<Map<String, Object>> asynCall(){
    .....
    return new AsyncResult<Map<String, Object>>(mapResultMap);
    }

}

我的春季配置是:

<task:annotation-driven executor="taskExecutor"  mode="aspectj" />

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="50" />
    <property name="maxPoolSize" value="300" />
    <property name="queueCapacity" value="30" />
</bean>

我的Mockito单元测试

@RunWith(MockitoJUnitRunner.class)
public class ConcurrentServiceTest{

    @InjectMocks
    private ConcurrentService concurrentService;

    @Mock(name = "taskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    @Test
    public void test1(){
        Assert.assertNotNull(concurrentService.createList();
    }
}

如果我执行此操作,我从java.lang.IllegalArgumentException: BeanFactory must be set on AnnotationAsyncExecutionAspect to access qualified executor 'taskExecutor'

获得了org.springframework.scheduling.aspectj.AbstractAsyncExecutionAspect.ajc$around$org_springframework_scheduling_aspectj_AbstractAsyncExecutionAspect

如果我删除注释中的限定符并离开@Async,则此测试运行完美,但如果我添加@Async("taskExecutor"),则会再次出现错误。

我相信Spring不需要运行,因为这只是单元测试,我该怎么做才能在Mockito中禁用aop或者我应该怎么做才能使用名称“taskExecutor”

0 个答案:

没有答案