8

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.

Community
  • 1
  • 1
user2052618
  • 376
  • 1
  • 3
  • 16
  • 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 http://stackoverflow.com/questions/8501975/reuse-spring-application-context-across-junit-test-classes – ninj Apr 03 '17 at 21:46
  • 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 ? – user2052618 Apr 05 '17 at 13:40
  • Maybe the examples in the spring docs do what you want? Try: [Context configuration with annotated classes](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-ctx-management-javaconfig) – ninj Apr 05 '17 at 21:44
  • 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. – user2052618 Apr 06 '17 at 19:37
  • 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? – ninj Apr 07 '17 at 21:57

1 Answers1

12

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/

Komal12
  • 3,119
  • 4
  • 13
  • 25
Filipe Luchini
  • 191
  • 2
  • 6
  • 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. – Filipe Luchini Oct 25 '19 at 21:28