我可以在Springboot中为我的Junit Test Case编写方面吗

发布于 2025-01-30 14:24:14 字数 870 浏览 3 评论 0原文

我有一个测试用例的分支,这些分支是在不同的测试场景/方法中反复使用相同的代码。就像Infra相关的维护代码。假设需要验证某些业务逻辑,然后才能执行一些与Instra相关的操作。执行业务逻辑之后,例如清洁所有与Infra相关的配置。认为我们可以使用方面。因为我们的主要服务是用Spring Boot编写的。

@TestInstance(Lifecycle.PER_CLASS)
@TestMethodOrder(OrderAnnotation.class)
@EnableAspectJAutoProxy(proxyTargetClass=true) 
public class RegressionTest{

    @Test
    @Order(1)
    public void Test1(){
    
    }
    
    @Test
    @Order(2)
    public void Test2(){
    
    }
    
    @Test
    @Order(3)
    public void Test3(){
    
    }
    
    @Test
    @Order(4)
    public void Test4(){
    
    }
    
    @Test
    @Order(5)
    public void Test5(){
    
    }
    
    @Test
    @Order(6)
    public void Test6(){
    
    }

}

在上面的示例中,假设在某些具有不同值的测试用例之前和之后要运行一些常见的代码(1,5使用一些具有不同活动的维护代码,而2,6使用一些不同的维护代码)。取决于测试案例。我打算使用方面,因此无需重复编写相同的代码。如果您有更好的建议,请建议您在这种情况下如何使用面向方面的编程。

I have a branch of Test cases that are using the same code repeatedly for different test scenarios/methods. It's like infra related maintenance code. Suppose need to validate some business logic before we need to perform some infra-related operation. Same after the execution of business logic like cleaning all the infra-related configs. Thought we can use Aspects. As our main services are written in spring boot.

@TestInstance(Lifecycle.PER_CLASS)
@TestMethodOrder(OrderAnnotation.class)
@EnableAspectJAutoProxy(proxyTargetClass=true) 
public class RegressionTest{

    @Test
    @Order(1)
    public void Test1(){
    
    }
    
    @Test
    @Order(2)
    public void Test2(){
    
    }
    
    @Test
    @Order(3)
    public void Test3(){
    
    }
    
    @Test
    @Order(4)
    public void Test4(){
    
    }
    
    @Test
    @Order(5)
    public void Test5(){
    
    }
    
    @Test
    @Order(6)
    public void Test6(){
    
    }

}

In the above example suppose want to run some common code before and after some test cases with the different values( 1 , 5 use some maintenance code with a different activity, and 2,6 use some different ). Depends on the test case. I planning to use Aspect so that no need to write same code repeatedly. Please suggest if you have a better suggestion or how can I use aspect-oriented programming in this scenario.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2025-02-06 14:24:14

您只能将Spring AOP应用于弹簧豆/组件。即,您的测试需要具有@component注释,以便通过相应配置的组件扫描拾取或在@bean @bean Factory方法中实例化。我不是春季用户,即我在使用测试的弹簧方面也没有太多经验。但是我所知道的是,您可以简单地使用本机extactJ,即通过编译时间或加载时间编织来应用,以实现所需的目标。实施这将是非常简单的。我当然可以为您提供帮助,如果您在github上提出 mcve ,所以我不必从头开始。

如果要使用非AOP解决方案,为什么不简单地使用现有的@beforeall和/或@beforeeach注释,就像 @asafb 以前建议?我认为这是不起作用的理由。

You can only apply Spring AOP to Spring beans/components. I.e., your test would need to e.g. have a @Component annotation in order to be picked up by a correspondingly configured component scan or be instantiated in a @Bean factory method. I am not a Spring user, i.e. I do not have much experience in using Spring from tests either. But what I do know is that you could simply use native AspectJ, either applied by compile-time or load-time weaving, in order to achieve what you want. That would be pretty straightforward to implement. I can certainly help you, if you present an MCVE on GitHub, so I do not have to start from scratch.

If you want to use a non-AOP solution, why not simply use the existing @BeforeAll and/or @BeforeEach annotations, just like @asafb suggested before? I see no reason why that would not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文