9

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.

3
  • Just execute the maven test goal or its gradle equivalent, if you're in gradle. This will run all tests from your project.
    – Aritz
    Commented May 13, 2017 at 8:07
  • you want to run only these two tests or is it okay if you run all tests?
    – pvpkiran
    Commented May 13, 2017 at 8:19
  • I am using Intellij IDE to run tests, I would like to run all test from one command or one parent class. Basically, it boils down on how to include separate test classes into one.
    – nomadus
    Commented May 13, 2017 at 9:32

2 Answers 2

13

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.

1
  • if you run the suite, does it initialize spring boot app multiple times? Commented Apr 5, 2018 at 18:33
6

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.