Java @可配置单元测试导致错误

Java @可配置单元测试导致错误,java,spring,maven,junit,configurable,Java,Spring,Maven,Junit,Configurable,类是编译时编织的。我在文档中读到,weaved类会对容器上下文之外构建的类发出警告,但不会出现完全错误,但我在maven构建中得到以下测试错误: testExecuteCommand(SportTimeExecutionCommandTest): Error creating bean with name 'SportTimeExecutionCommand': Injection of resource dependencies failed; nested exception is org.

类是编译时编织的。我在文档中读到,weaved类会对容器上下文之外构建的类发出警告,但不会出现完全错误,但我在maven构建中得到以下测试错误:

testExecuteCommand(SportTimeExecutionCommandTest): Error creating bean with name 'SportTimeExecutionCommand': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'timeStrategyAnalyzer' is defined
尽管我使用Mockito junit类运行程序运行测试,但仍然存在这种情况

@RunWith(MockitoJUnitRunner.class)
public class SportTimeExecutionCommandTest {

    private static final Integer SPORT_ID = 1; 


    @Mock
    private IdTimeRequestAdjuster idTimeRequestAdjuster ; 
    @Mock
    private StrategyAnalyzer<List<TimeStep>> strategyAnalyzer;
    @Mock
    private SportStepExecutor stepExecutor ; 
    @Mock
    private TimeRequestStrategy strategy ; 
    @Mock 
    private TimeStep step; 

    @Mock
    private Future<List<EventData>> future; 

    private SportTimeExecutionCommand command; 


    private List<TimeStep> steps = new ArrayList<TimeStep>(); 

    @Before
    public void setUp() throws Exception {

        command = new SportTimeExecutionCommand(SPORT_ID); 
        command.setIdTimeRequestAdjuster(idTimeRequestAdjuster);
        command.setStepExecutor(stepExecutor); 
        command.setStrategyAnalyzer(strategyAnalyzer); 
        when(idTimeRequestAdjuster.getCurrentValue(SPORT_ID)).thenReturn(strategy);
        when(strategyAnalyzer.calculateSteps(strategy)).thenReturn(steps); 
        steps.add(step); 
        when(stepExecutor.executeStep(SPORT_ID, step)).thenReturn(future); 
        when(future.get()).thenReturn(new ArrayList<EventData>()); 
    }

    @Test
    public void testExecuteCommand() throws InterruptedException, ExecutionException {
        command.executeCommand(); 
        verify(idTimeRequestAdjuster).getCurrentValue(SPORT_ID);
        verify(strategyAnalyzer).calculateSteps(strategy); 
        verify(stepExecutor).executeStep(SPORT_ID,step); 

    }

}
@RunWith(MockitoJUnitRunner.class)
公共类SportTimeExecutionCommandTest{
私有静态最终整数SPORT_ID=1;
@嘲弄
私有idtimerequestrator idtimerequestrator;
@嘲弄
私人战略分析仪;
@嘲弄
私人体育步进执行人;
@嘲弄
私有时间请求策略;
@嘲弄
私有时间步长;
@嘲弄
私人未来;
专用SportTimeExecutionCommand命令;
私有列表步骤=新建ArrayList();
@以前
public void setUp()引发异常{
命令=新的SportTimeExecutionCommand(运动ID);
setIDTimeRequestRegulator(IDTimeRequestRegulator)命令;
命令。设置步骤执行器(步骤执行器);
命令setStrategyAnalyzer(策略分析器);
当(IDTimeRequestAdjulator.getCurrentValue(SPORT_ID)),然后返回(策略);
当(strategyAnalyzer.calculateSteps(strategy)),然后返回(步骤);
步骤。添加(步骤);
当(stepExecutor.executeStep(SPORT_ID,step)),然后返回(future);
when(future.get()).thenReturn(newArrayList());
}
@试验
public void testExecuteCommand()引发InterruptedException、ExecutionException{
command.executeCommand();
验证(IDTimeRequestRegulator).getCurrentValue(运动ID);
验证(策略分析器)。计算(策略);
验证(步骤执行器)。执行步骤(运动ID,步骤);
}
}
可供参考的实现类:

@Configurable(autowire=Autowire.BY_TYPE)
public class SportTimeExecutionCommand implements AdjustingCommand<List<EventData>,Integer>{

    private final static Logger logger = Logger.getLogger(SportTimeExecutionCommand.class.getName());

    @Autowired
    private IdTimeRequestAdjuster idTimeRequestAdjuster ; 

    @Resource(name = "timeStrategyAnalyzer")
    private StrategyAnalyzer<List<TimeStep>> strategyAnalyzer;

    @Autowired
    private SportStepExecutor stepExecutor ; 

    private final Integer sportId ; 

    public SportTimeExecutionCommand(Integer sportId)
    {
        this.sportId = sportId ;
    }

    @Override
    public List<EventData> executeCommand() throws InterruptedException, ExecutionException {
        List<EventData> eventData = new ArrayList<EventData>();
        List<TimeStep> timeSteps = strategyAnalyzer.calculateSteps(idTimeRequestAdjuster.getCurrentValue(sportId));
        List<Future<List<EventData>>> futureResults = new ArrayList<Future<List<EventData>>>();

        for (TimeStep timeStep : timeSteps) {
            futureResults.add(stepExecutor.executeStep(sportId, timeStep));
        }

        for (Future<List<EventData>> futureEventData : futureResults) {
            eventData.addAll(futureEventData.get());
        }


        return eventData;
    }

    @Override
    public Integer getCriteria() {
        return sportId; 
    }

    @Override
    public void adjust() {
        logger.warning("adjusting sportId "+sportId+" value is now : "+idTimeRequestAdjuster.getCurrentValue(sportId).getRequests()); 
        idTimeRequestAdjuster.adjustUp(sportId);
    }

    public void setIdTimeRequestAdjuster(IdTimeRequestAdjuster idTimeRequestAdjuster) {
        this.idTimeRequestAdjuster = idTimeRequestAdjuster;
    }

    public void setStrategyAnalyzer(StrategyAnalyzer<List<TimeStep>> strategyAnalyzer) {
        this.strategyAnalyzer = strategyAnalyzer;
    }

    public void setStepExecutor(SportStepExecutor stepExecutor) {
        this.stepExecutor = stepExecutor;
    }

}
@可配置(autowire=autowire.BY_类型)
公共类SportTimeExecutionCommand实现调整命令{
私有最终静态记录器Logger=Logger.getLogger(SportTimeExecutionCommand.class.getName());
@自动连线
私有idtimerequestrator idtimerequestrator;
@资源(name=“timeStrategyAnalyzer”)
私人战略分析仪;
@自动连线
私人体育步进执行人;
私有最终整数sportId;
公共SportTimeExecutionCommand(整数sportId)
{
this.sportId=sportId;
}
@凌驾
public List executeCommand()引发InterruptedException,ExecutionException{
List eventData=new ArrayList();
List timeSteps=strategyAnalyzer.calculateSteps(idtimerequestjugger.getCurrentValue(sportId));
List futureResults=new ArrayList();
for(TimeStep TimeStep:timeSteps){
添加(stepExecutor.ExecuteTep(sportId,timeStep));
}
用于(未来事件数据:未来结果){
addAll(futureEventData.get());
}
返回事件数据;
}
@凌驾
公共整数getCriteria(){
返回sportId;
}
@凌驾
公共空间调整(){
警告(“正在调整sportId”+sportId+”值现在为:“+IDTimeRequestRegulator.getCurrentValue(sportId.getRequests());
IDTimeRequestRegulator.adjustUp(运动ID);
}
public void setIDTimeRequestRegulator(IDTimeRequestRegulator IDTimeRequestRegulator){
this.idtimerequestrator=idtimerequestrator;
}
公共无效设置策略分析器(策略分析器){
this.strategyAnalyzer=strategyAnalyzer;
}
公共无效设置步骤执行器(运动步骤执行器步骤执行器){
this.stepExecutor=stepExecutor;
}
}
有人能看到问题吗


编辑:我怀疑它首先处理资源,这就是这里发生错误的原因。如果我将注释切换到autowired,那么它将在第一个autowiredbean上中断

我认为问题是由
@Resource
注释中的
name
属性引起的:

@Resource(name = "timeStrategyAnalyzer")
private StrategyAnalyzer<List<TimeStep>> strategyAnalyzer;
@Resource(name=“timeStrategyAnalyzer”)
私人战略分析仪;
也许,如果您删除它,由方面执行的依赖项注入将很好地工作-请注意,实际上您不会在您发布的代码中的任何地方公开具有此名称的资源。
在Spring容器外部构造的编织类(使用
new
操作符)也可以进行依赖注入。

我的解决方案是运行一个单独的maven构建概要文件,该概要文件在编译时不使用方面,然后在集成阶段使用方面编译

在发布问题之前,我尝试删除限定符。此外,我尝试使用自动连线作为替代,但都没有解决问题。timeStrategyAnalyzer是一个有效的bean,我只是没有显式地发布它以避免混淆问题。我还对这里的spring交互感到困惑。就我所见,我没有显式地启动spring容器(它使用mockito junit runner运行),那么上下文将如何配置呢?这可能是因为您有编译时编织-方面尝试注入依赖项,但spring容器没有运行。你能试着切换到运行时编织吗?我相信运行时编织可以工作(基于7.8.1.1单元测试@可配置对象),但我认为这不是强制这种方法的好方法。Ok解决了问题。测试在我从测试上下文中排除的目录中查找bean。我仍然不确定它是如何与上下文相匹配的。它是否会把它们全部积累起来,然后寻找所有的豆子?它是否有某种管理冲突的方式?还有,为什么它不能按照7.8.1.1单元测试@可配置对象中的描述工作。是春虫吗?