【问题标题】:Test Suite Run Spring Boot Once测试套件运行一次 Spring Boot
【发布时间】:2017-08-28 18:10:42
【问题描述】:

我正在尝试创建一个在套件开始时运行 Spring Boot 的测试套件。我让每个测试用例都有@SpringBootTest,但我只想在测试套件中有@SpringBootTest。

我确实看到了this,但没有提到@RunWith Suite.class。

【问题讨论】:

  • 您是否关心为每个测试启动一个新的应用程序上下文?如果您不更改定义,Spring 测试框架会缓存应用程序上下文。见stackoverflow.com/questions/8501975/…
  • 您提供的信息很好,但我没有上下文 xml(所有注释)。我如何做相当于将所有测试设置为使用相同的上下文而没有为 @ContextConfiguration 设置的 xml 文件?
  • 也许春季文档中的示例可以满足您的需求?试试看:Context configuration with annotated classes
  • 谢谢。我看到了 ContextConfiguration 的注释并将其放入,但每次新测试用例开始时,我仍然在控制台中看到用特殊字符编写的“spring”。
  • 恐怕很难弄清楚发生了什么。您能否发布一个缩减版的测试套件来演示该问题?

标签: spring spring-boot test-suite


【解决方案1】:

如果我理解您的问题,对于您使用 spring boot 启动许多测试,您可以执行以下操作:

1) 首先创建你的测试类。这里我有第一个测试类:

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
public class ExampleRepositoryTests {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private CustomerRepository repository;

    @Test
    public void testExample() throws Exception {
        this.entityManager.persist(new Customer("sboot", "1234"));
        Customer user = repository.findByFirstName("sboot").get(0);
        assertThat(user.getFirstName()).isEqualTo("sboot");
    }
}

2) 我的第二个测试课。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
public class ExampleRepositoryTests2 {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private CustomerRepository repository;

    @Test
    public void testExample() throws Exception {
        this.entityManager.persist(new Customer("sboot", "1234"));
        Customer user = repository.findByFirstName("sboot").get(0);
        assertThat(user.getFirstName()).isEqualTo("sboot");
    }
}

3) 现在让我们创建套件测试类:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    ExampleRepositoryTests.class, //test case 1
    ExampleRepositoryTests2.class     //test case 2
})
public class AppTest {

}

您可以单独启动每个测试,但是,如果您启动套件测试,该类将启动在 @Suite.SuiteClasses 中声明的每个测试。 这些测试我只使用 Spring JPA 和 Spring Boot。重要的是,您的项目中有依赖项。下面你可以看到我的 maven 依赖项:

<dependencies>  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>       
    </dependency>        
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>       
    </dependency>       
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>      
</dependencies>

请注意,我正在测试 JPA 数据类 (@DataJpaTest)。对于其他测试类型,您将使用其他 Spring 注释。您可以查看有关此here 的一些文档。 希望能帮到你! o/

【讨论】:

  • 视情况而定。在这个例子中,我不需要使用 SpringBootTest,因为测试使用了 DataJpaTest。但是,如果您不使用一些注释来为您的测试提供一些上下文(如 DataJpaTest),那么您将需要使用通常的 SpringBootTest 提供一些上下文。您可以认为 DataJpaTest 是使用 SpringBootTest 的数据测试的简单上下文准备测试的快捷方式。
猜你喜欢
  • 2019-04-13
  • 2023-01-22
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2017-02-20
  • 1970-01-01
相关资源
最近更新 更多