Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用方面时如何运行Spring集成测试?_Java_Spring Mvc_Spring Aop_Spring Test - Fatal编程技术网

Java 使用方面时如何运行Spring集成测试?

Java 使用方面时如何运行Spring集成测试?,java,spring-mvc,spring-aop,spring-test,Java,Spring Mvc,Spring Aop,Spring Test,我正在开发一个SpringMVCWeb应用程序(目前是Java6和Spring3.0.6)。 我开始使用Junit4编写一些Spring集成测试,这些测试扩展了AbstractTransactionalJUnit4SpringContextTests。我通过Maven构建或者在EclipseIDE(3.7)中调用它们。这些测试调用控制器方法(即在用@Controller注释的类中用@RequestHandler注释的方法) 在我将基于方面的登录添加到控制器之前,一切都进展顺利: // p

我正在开发一个SpringMVCWeb应用程序(目前是Java6和Spring3.0.6)。 我开始使用Junit4编写一些Spring集成测试,这些测试扩展了AbstractTransactionalJUnit4SpringContextTests。我通过Maven构建或者在EclipseIDE(3.7)中调用它们。这些测试调用控制器方法(即在用@Controller注释的类中用@RequestHandler注释的方法)

在我将基于方面的登录添加到控制器之前,一切都进展顺利:

    // public controller methods
@Pointcut("execution(public * com.axiope.webapp.controller.*.*(..))")
private void publicControllerMethod() {
}
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
private void requestHandler(){}

@Pointcut("publicControllerMethod() && requestHandler() ")
private void controllerHandler(){}



// logs contoller exceptions
@AfterThrowing(
        pointcut="controllerHandler()",
        throwing="ex")
      public void logControllerExceptions(Throwable ex) {
    logger = LogFactory.getLog(ex.getClass());
    logger.error("Controller exception !" + ex.getMessage());   
    }
现在,当我通过Maven运行测试时,会出现如下错误:

No unique bean of type [com.axiope.webapp.controller.StructuredDocumentController]
is defined: expected single bean but found 0: 
在测试中,我将从setUp方法中的applicationContext加载控制器:

structuredDocumentController = applicationContext.getBean(
    StructuredDocumentController.class);
如果我注释掉该方面,则不会发生此错误。我怀疑这与Spring代理控制器有关,然后控制器类无法通过其类名识别。我已经尝试在applicationContext.xml中将控制器声明为bean,但这没有帮助。在Eclipse中运行测试时也会出现此问题,因此我的Maven配置没有问题

我的问题是:如何在测试中检测控制器bean

如果有任何帮助,我将不胜感激——在控制器类中向方法添加方面是否错误?我应该在测试时以某种方式禁用方面吗?(虽然在理想情况下,我希望在集成测试中看到日志正常工作)

非常感谢


Richard

方面不是可能的问题,您应该能够在控制器中使用它们,而不会出现任何问题


我的猜测是,您没有为测试加载正确的上下文-您如何指定用于此测试的应用程序上下文-您的测试类上是否有
@ContextConfiguration
,位置是根上下文(通过ContextLoaderListener指定的)还是Web应用程序上下文(一个通过DispatcherServlet指定)。

谢谢-是上下文配置指向所有必需的Spring配置文件。例如,@ContextConfiguration(位置={“classpath:/applicationContext resources.xml”,“classpath:/applicationContext dao.xml”,“classpath:/applicationContext service.xml”、“classpath:/applicationContext test.xml”、“/WEB-INF/applicationContext*.xml”、“/WEB-INF/dispatcher servlet.xml”})要补充的是,这些文件是在web.xml的contextConfigLocation servlet上下文参数中指定的哦,1个问题,您是使用AspectJ编译时/加载时编织还是使用带有@AspectJ样式注释的Spring AOP。您是对的,控制器的代理可能是导致问题的原因-您派生了控制器吗来自某个接口/抽象类的?Spring AOP AspectJ注释。控制器没有实现任何接口(控制器注释都在实现方法上)。我无法重现您看到的行为-您可以尝试一件事吗-您可以尝试使用
@RunWith(SpringJunit4Runner.class)而不是AbstractTransactionalJUnit4SpringContextTests吗
@ContextConfiguration
与您之前和在类中指定的内容
@Autowired-StructuredDocumentController-documentController