其他分享
首页 > 其他分享> > 测试套件运行一次Spring Boot

测试套件运行一次Spring Boot

作者:互联网

我正在尝试创建一个在套件开始时运行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-2,test-suite
来源: https://codeday.me/bug/20190522/1154313.html