问题描述
我正在尝试创建一个在套件开始时运行一次Spring Boot的测试套件.我的工作原理是每个测试用例都有@SpringBootTest,但我只想在测试套件中使用@SpringBootTest.
I am trying to create a test suite that runs Spring Boot once at the start of the suite. I have it working such that each test case has @SpringBootTest but I'd like to have @SpringBootTest in the test suite only.
我确实看到了此,但是没有提到@ RunWith Suite.class.
I did see this but that didn't mentioned @RunWith Suite.class.
推荐答案
如果我理解您的问题,那么您可以使用Spring Boot启动许多测试,您可以执行以下操作:
If I understood your question, for you launch many tests with spring boot you can do something like this:
1)首先创建您的测试类.这是我的第一堂课:
1) First create yours tests classes. Here I have the first test class:
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)现在让我们创建套件测试类:
3) Now let's create the suite test class:
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依赖项:
You can start each test separately, but, if you start the suite test, the class will start every tests declared in @Suite.SuiteClasses.These tests I am using just Spring JPA and Spring Boot. It is importante you have the dependencies in your project. Below you can see my maven dependencies:
<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注释.您可以在此处.希望对您有帮助! o/
Note that I am testing JPA Data Classes (@DataJpaTest). For others test types you will using others Spring annotations. You can see some documentation about this here.I hope help you! o/
这篇关于测试套件一次运行Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!