Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple tests with Spring Boot

Tags:

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.

like image 290
nomadus Avatar asked May 13 '17 07:05

nomadus


People also ask

Does Spring run tests in parallel?

Starting with JUnit 4, tests can be run in parallel to gain speed for larger suites. The problem was concurrent test execution was not fully supported by the Spring TestContext Framework prior to Spring 5. In this quick article, we'll show how to use Spring 5 to run our tests in Spring projects concurrently.

How do you write unit test cases for REST API in Spring boot?

React Full Stack Web Development With Spring Boot Spring Boot provides an easy way to write a Unit Test for Rest Controller file. With the help of SpringJUnit4ClassRunner and MockMvc, we can create a web application context to write Unit Test for Rest Controller file.

What is Spring boot MockMvc?

MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. Let's see how to use it: private MockMvc mockMvc; @BeforeEach public void setup() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build(); }


1 Answers

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.

like image 131
nomadus Avatar answered Sep 23 '22 05:09

nomadus