当前位置: 首页 > 知识库问答 >
问题:

@BeForeClass和Spring@TestExecutionListener beforeTestClass()有什么不同

丰赞
2023-03-14

Maven依赖项:
spring-core:3.0.6。发布
spring-context:3.0.6。发布
spring-test:3.0.6。发布
spring-data-commons-core:1.2.0.m1
spring-data-mongoDB:1.0.0.m4
mongo-java-driver:2.7.3
junit:4.9
cglib:2.2

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = { "classpath:test-config.xml" })
public class TestNothing extends AbstractJUnit4SpringContextTests {

    @Autowired
    PersonRepository repo;

    @BeforeClass
    public static void runBefore() {
        System.out.println("@BeforeClass: set up.");
    }

    @Test
    public void testInit() {
        Assert.assertTrue(repo.findAll().size() == 0 );
    }
}

=> @BeforeClass: set up.
=> Process finished with exit code 0

(1)重写beforeTestClass(TextContext testContext):

import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.AbstractTestExecutionListener;

public class BeforeClassHook extends AbstractTestExecutionListener {

    public BeforeClassHook() { }

    @Override
    public void beforeTestClass(TestContext testContext) {
        System.out.println("BeforeClassHook.beforeTestClass(): set up.");
    }
}

(2)使用@TestExecutionListeners注释:

import org.springframework.test.context.TestExecutionListeners;  
// other imports are the same    

@ContextConfiguration(locations = { "classpath:test-config.xml" })
@TestExecutionListeners(BeforeClassHook.class)
public class TestNothing extends AbstractJUnit4SpringContextTests {

    @Autowired
    PersonRepository repo;

    @Test
    public void testInit() {
        Assert.assertTrue(repo.findAll().size() == 0 );
    }
}

=> BeforeClassHook.beforeTestClass(): set up.
=> Process finished with exit code 0

共有1个答案

闽承望
2023-03-14

TestExecutionListeners是一种将可重用代码外部化的方法,可重用代码用于测试。

因此,如果实现TestExecutionListener,您可以根据需要跨测试类层次结构并可能跨项目重用它。

另一方面,@beforeClass方法自然只能在单个测试类层次结构中使用。

所以这真的取决于你的用例。如果您只需要在单个测试类或单个测试类层次结构中使用“before class”功能,那么您最好选择实现@beforeclass方法的简单路线。但是,如果您预见到在不同的测试类层次结构或跨项目中需要“类前”功能,那么您应该考虑实现自定义TestExecutionListener或JUnit规则。

SpringTestExecutionListener相对于JUnit规则的好处是,TestExecutionListener可以访问TestContext,因此可以访问SpringApplicationContext,而JUnit规则无法访问SpringApplicationContext。此外,可以自动发现并排序TestExecutionListener

相关资源:

  • SPR-8854

问候,

Sam(Spring TestContext框架的作者)

 类似资料:
  • 这是一个取自现有代码库的cucumber场景大纲,但我被要求使用TestNG尝试它。一些东西(场景(),场景(),TestNGCucumberRunner,数据提供商)已经从stackoverflow上的另一个帖子中删除,因为我不知道如何使用TestNG使场景大纲发挥良好。 在执行时,唯一的错误是NullPointerException,因为WebDriver获取其值时@BeforeClass未触

  • 我想知道Spring boot JPA是如何使用hibernate的,Spring boot JPA和hibernate之间有什么关系吗?当我们使用boot jpa时,我们只是实现了org.springframework.data.jpa.repository.jparepository接口,可以使用与列名匹配的函数名编写查询,也可以编写自定义查询,那么hibernate在这里的角色是什么?我们怎

  • 问题内容: 我有以下代码: 以及其他各种方法,例如@ Before,@ After,@ Test或@AfterClass方法。 测试在启动时不会像看起来的那样失败。有谁可以帮助我吗? 我有JUnit 4.5 该方法无法立即调用注释为@before的setUp()。类def是: 问题答案: 不要扩展TestCase并同时使用注释! 如果需要使用批注创建测试套件,请使用RunWith批注,例如: (按

  • 这两个注释都用于表示带注释的方法应该在当前测试类中的每个测试方法之前执行。 那么为什么我们要将注释从@BeforeClass-junit4更改为@beforecreach-Junit 5呢? JUnit5中添加了什么我没有的东西? 其他注释的情况类似。

  • 本文向大家介绍getComputedStyle和element.style有什么不同?相关面试题,主要包含被问及getComputedStyle和element.style有什么不同?时的应答技巧和注意事项,需要的朋友参考一下 element.style 只能获取内联样式属性 getComputedStyle() 可以获取所有样式属性

  • 本文向大家介绍frame和bounds有什么不同?相关面试题,主要包含被问及frame和bounds有什么不同?时的应答技巧和注意事项,需要的朋友参考一下 答案:frame指的是:该view在父view坐标系统中的位置和大小。(参照点是父亲的坐标系统) bounds指的是:该view在本身坐标系统中 的位置和大小。(参照点是本身坐标系统)