Test Suite Run Spring Boot Once
Asked Answered
P

1

15

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.

Precedent answered 3/4, 2017 at 18:42 Comment(5)
Are you concerned with starting up a new application context for every test? Spring test framework caches the application context if you don't change the definition. See #8502475Levitical
The info you gave is good but i have no context xml (all annotations.) How to i do the equivalent of setting all the tests to use the same context with no xml file to set for @ContextConfiguration ?Precedent
Maybe the examples in the spring docs do what you want? Try: Context configuration with annotated classesLevitical
thanks. i see the annotation for ContextConfiguration and put it in but i still see "spring" written with the special characters in my console every time a new test case starts.Precedent
Bit tricky to figure out what's going on I'm afraid. Could you perhaps post a cut-down version of your test suite to demonstrate the problem?Levitical
S
17

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/

Saintsimon answered 31/10, 2017 at 10:51 Comment(1)
It's depend. In this example I did not need to use SpringBootTest because the test uses DataJpaTest. But if you do not use some annotation to provide some context to your test (like DataJpaTest) you will need provide some context using normally SpringBootTest. You can think that DataJpaTest is a shortcut for prepare your test with a simple context for data tests insted of use SpringBootTest.Saintsimon

© 2022 - 2024 — McMap. All rights reserved.