nomadus
nomadus

Reputation: 909

How to run multiple tests with Spring Boot

With Spring Boot 1.5, how could I run multiple tests which are in different classes?

E.g.

I have `Service1` tests in `Service1test.java`;

I have `Service2` tests in `Service2test.java`;

I shall need to run both in one go.

Upvotes: 9

Views: 20869

Answers (2)

nomadus
nomadus

Reputation: 909

What I have done is as follows: In the main class

@RunWith(Suite.class)
@Suite.SuiteClasses({
        PostServiceTest.class,
        UserServiceTest.class
})
public class DataApplicationTests {
    @Test
    public void contextLoads() {
    }
}

In the PostServiceTest I have

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class PostServiceTest  {
    @Autowired
    IPostService postService;

    @Before
    public void initiate() {
        System.out.println("Initiating the before steps");
    }

    @Test
    public void testFindPosts() {
        List<Post> posts= postService.findPosts();
        Assert.assertNotNull("failure - expected Not Null", posts);       
    }
}

The second class, UserServiceTest has similar structure.

When I run the DataApplicationTests, it runs both the classes.

Upvotes: 13

ntakouris
ntakouris

Reputation: 958

I will assume you are using IntelliJ, but the same stuff apply for all the IDEs.

Gradle and Maven have got a standarized project structure, that means all Test classes positioned inside the 'test-root' will be ran on either mvn test (to just test), or while you build (to check wether the code behaves correctly. In that case, if a test fails, build fails too).

Here's an image of a marked-green test directory on IntelliJ :enter image description here

Your IDE should allow you to run specific tests, test suites or classes seperately, without the need to type out any command. IntelliJ provides some Icons on the separator column thingy (near to the line numbers) that enables you to run that specific stuff. Check out these green play buttons:enter image description here

Be careful with creating test suites though. That way unless you manually configure the tests that need to be run, you will get duplicate runs because the build tools will run all the test suites independently and then all the tests! (That means that if test suite A contains test suite B and C, B and C are going to be ran 2 times: 1 each from A, and 1 each independently. The same applies for standalone test classes).

Upvotes: 6

Related Questions