Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Suite Run Spring Boot Once

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.

I did see this but that didn't mentioned @RunWith Suite.class.

like image 241
user2052618 Avatar asked Apr 03 '17 18:04

user2052618


1 Answers

If I understood your question, for you launch many tests with spring boot you can do something like this:

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) My second test class.

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) 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 {

}

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>

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/

like image 109
Filipe Luchini Avatar answered Sep 21 '22 16:09

Filipe Luchini