如何在Spring MVC应用程序中测试Aspect

VadOs

我有一个Spring MVC应用程序,在其中我使用Aspect来捕获所有控制器方法中的异常

@Component
@Aspect
public class ControllerExceptionAspect {

    private Logger logger;

    public ControllerExceptionAspect() {
       logger = Logger.getLogger(ControllerExceptionAspect.class);
    }

    public ControllerExceptionAspect(Logger logger) {
       this.logger = logger;
    }

    // Catching all exceptions from all methods in all controllers classes

    @AfterThrowing(pointcut = "execution(* com.my.package..controller..*(..))", throwing = "exception")
    public void afterThrowingAdvice(Exception exception) {
       logger.error("CONTROLLER ASPECT: EXCEPTION IN METHOD -> " +    
       exception.getClass());
    }
}

Aspect可以正常工作,但是很遗憾,我无法对其进行测试。我尝试了很多次,但是在Controller中模拟异常后却无法获取如何捕获Aspect方法的信息

@SuppressWarnings("ALL")
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({
        @ContextConfiguration(classes = RootConfig.class),
        @ContextConfiguration(classes = WebConfig.class)
})
public class ControllerExceptionAspectTest {

    @Autowired
    ApplicationContext applicationContext;

    @Test
    public void testControllerExceptionAspectGetsExecutedWhenExceptionOccures(){
        HomeController homeController = (HomeController)applicationContext.getAutowireCapableBeanFactory().getBean("homeController");
        try{homeController.callMethod("00000");}
        catch (Exception e){}
        ControllerExceptionAspect controllerExceptionAspect = (ControllerExceptionAspect)applicationContext.getAutowireCapableBeanFactory().getBean("controllerExceptionAspect");
        // HOW TO CATCH THAT ASPECT METHOD WAS CALLED???
    }
}
加马斯洛夫斯基

我认为您想要实现的是测试您创建的配置(方面切入点),而不是可以进行单元测试的方面本身。恐怕没有简单的方法可以实现这一目标。

您可以遵循一些有关捕获日志或其他想法的互联网建议。老实说,仅当您确实需要测试Aspect的预期行为时,我才测试它的预期行为。如果正在记录日志,我不会这样做。如果它为数据库设置了某些功能(或其他副作用),我将验证该值是否在数据库中。那就是集成测试的草草地面。

如果确实需要按自己的方式测试方面,则可以编写类似于给定代码的内容。但是请记住,正常的(非测试)运行时spring配置将需要Spring上下文中存在的Verifier接口的虚拟实现。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Config.class)
public class AspectTesting {

    @Autowired
    ServiceWithAspect service;

    @Autowired
    Verifier verifyingAspect;

    @Test
    public void test() {
        // given
        boolean condition = false;

        // when
        try {
            service.doit();
        } catch (Exception swallow) {}

        // then
        try {
            condition = ((VerifyingAspect) ((Advised) verifyingAspect).getTargetSource().getTarget()).wasExecuted();
        } catch (Exception swallow) {}

        // then
        Assert.assertTrue(condition);
    }
}

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("aspects")
class Config {
}

@Component
class VerifyingAspect implements Verifier {

    private boolean executed = false;

    public boolean wasExecuted() {
        return executed;
    }

    @Override
    public void invoked() {
        executed = true;
    }
}

@Service
class ServiceWithAspect {
    public void doit() {
        throw new RuntimeException();
    }
}

@Component
@Aspect
class TestedAspect {

    @Autowired
    Verifier verifier;

    @AfterThrowing(pointcut = "execution(* *(..))", throwing = "exception")
    public void afterThrowingAdvice(Exception exception) {
        // your aspect logic here
        verifier.invoked();
    }
}

interface Verifier {
    void invoked();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spring MVC应用程序测试中的ContextConfiguration

来自分类Dev

如何在Spring MVC(非Spring Boot)应用程序中自定义Jackson

来自分类Dev

如何在android中的应用程序订阅中测试

来自分类Dev

如何在应用程序jar中包含我的测试

来自分类Dev

在 Spring MVC 应用程序中编写测试用例的标准做法是什么?

来自分类Dev

Spring MVC应用程序中的性能

来自分类Dev

Spring MVC应用程序中的性能

来自分类Dev

如何在Spring MVC应用程序中为不同的URL加载CSS

来自分类Dev

如何在Spring 3 MVC应用程序中对文件上传实施病毒扫描

来自分类Dev

Spring MVC应用程序中如何在Hibernate中使用Jboss数据源

来自分类Dev

如何在Spring MVC应用程序的Hibernate中打印生成的SQL?

来自分类Dev

如何在Spring MVC应用程序中终止Firebase线程

来自分类Dev

如何在Spring 3 MVC应用程序中对文件上传实施病毒扫描

来自分类Dev

如何在 Spring Framework MVC 应用程序中嵌入 Tomcat?

来自分类Dev

Spring MVC应用程序Junit测试用例失败

来自分类Dev

如何在WebForms应用程序中启用MVC支架?

来自分类Dev

如何在午夜在MVC应用程序中刷新数据?

来自分类Dev

如何在Spring Boot应用程序中配置PageableHandlerMethodArgumentResolver

来自分类Dev

如何在Spring应用程序中监视JMS队列?

来自分类Dev

如何在MVC应用程序中处理应用程序池配置

来自分类Dev

如何在Spring Web应用程序运行时在spring-web-mvc中获取WebApplicationContext和DispatcherServlet的实例

来自分类Dev

如何在Spring Web应用程序运行时在spring-web-mvc中获取WebApplicationContext和DispatcherServlet的实例

来自分类Dev

如何在ASP.NET MVC多层应用程序中正确进行单元测试?

来自分类Dev

如何在ASP.NET MVC多层应用程序中正确进行单元测试?

来自分类Dev

如何在现有的spring-mvc应用程序中集成spring-integration?

来自分类Dev

Spring Boot中多战应用程序的集成测试

来自分类Dev

如何在运行时在Spring MVC应用程序中创建动态数据库连接?

来自分类Dev

如何在Java配置文件(不是XML)中启用Spring MVC应用程序支持的异步

来自分类Dev

如何在Spring-MVC和JSF混合Web应用程序中设置JSF欢迎文件

Related 相关文章

  1. 1

    Spring MVC应用程序测试中的ContextConfiguration

  2. 2

    如何在Spring MVC(非Spring Boot)应用程序中自定义Jackson

  3. 3

    如何在android中的应用程序订阅中测试

  4. 4

    如何在应用程序jar中包含我的测试

  5. 5

    在 Spring MVC 应用程序中编写测试用例的标准做法是什么?

  6. 6

    Spring MVC应用程序中的性能

  7. 7

    Spring MVC应用程序中的性能

  8. 8

    如何在Spring MVC应用程序中为不同的URL加载CSS

  9. 9

    如何在Spring 3 MVC应用程序中对文件上传实施病毒扫描

  10. 10

    Spring MVC应用程序中如何在Hibernate中使用Jboss数据源

  11. 11

    如何在Spring MVC应用程序的Hibernate中打印生成的SQL?

  12. 12

    如何在Spring MVC应用程序中终止Firebase线程

  13. 13

    如何在Spring 3 MVC应用程序中对文件上传实施病毒扫描

  14. 14

    如何在 Spring Framework MVC 应用程序中嵌入 Tomcat?

  15. 15

    Spring MVC应用程序Junit测试用例失败

  16. 16

    如何在WebForms应用程序中启用MVC支架?

  17. 17

    如何在午夜在MVC应用程序中刷新数据?

  18. 18

    如何在Spring Boot应用程序中配置PageableHandlerMethodArgumentResolver

  19. 19

    如何在Spring应用程序中监视JMS队列?

  20. 20

    如何在MVC应用程序中处理应用程序池配置

  21. 21

    如何在Spring Web应用程序运行时在spring-web-mvc中获取WebApplicationContext和DispatcherServlet的实例

  22. 22

    如何在Spring Web应用程序运行时在spring-web-mvc中获取WebApplicationContext和DispatcherServlet的实例

  23. 23

    如何在ASP.NET MVC多层应用程序中正确进行单元测试?

  24. 24

    如何在ASP.NET MVC多层应用程序中正确进行单元测试?

  25. 25

    如何在现有的spring-mvc应用程序中集成spring-integration?

  26. 26

    Spring Boot中多战应用程序的集成测试

  27. 27

    如何在运行时在Spring MVC应用程序中创建动态数据库连接?

  28. 28

    如何在Java配置文件(不是XML)中启用Spring MVC应用程序支持的异步

  29. 29

    如何在Spring-MVC和JSF混合Web应用程序中设置JSF欢迎文件

热门标签

归档