use*_*618 8 spring test-suite spring-boot
我正在尝试创建一个在套件开始时运行Spring Boot的测试套件.我有它的工作,每个测试用例都有@SpringBootTest,但我想在测试套件中只有@SpringBootTest.
我确实看到了这个,但没有提到@RunWith Suite.class.
小智 9
如果我理解您的问题,那么您可以使用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");
}
}
Run Code Online (Sandbox Code Playgroud)
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");
}
}
Run Code Online (Sandbox Code Playgroud)
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 {
}
Run Code Online (Sandbox Code Playgroud)
您可以分别启动每个测试,但是,如果启动套件测试,则该类将启动@ 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>
Run Code Online (Sandbox Code Playgroud)
请注意,我正在测试JPA数据类(@DataJpaTest)。对于其他测试类型,您将使用其他Spring注释。您可以在此处查看有关此内容的一些文档。希望对您有帮助!o /
| 归档时间: |
|
| 查看次数: |
8464 次 |
| 最近记录: |