我正在尝试创建一个在套件开始时运行 Spring Boot 的测试套件。我让它工作,每个测试用例都有@SpringBootTest,但我只想在测试套件中有@SpringBootTest。
我确实看到了 this但这并没有提到@RunWith Suite.class。
最佳答案
如果我理解您的问题,那么您可以使用 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/
关于spring - 测试套件运行 Spring Boot 一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192046/