简体   繁体   中英

Spring boot Test multiple classes for API and repository

I write the test for the Spring Boot and have 2 classes that are testing the API and the repository. The skeletons are provided below,

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class AppointmentAPITest {


    /*
     * we need a system to simulate this behavior without starting a
     * full HTTP server. MockMvc is the Spring class that does that.
     * */
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private AppointmentAPI api;


    // now the tests are going on

}

The repository test class,

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class AppointmentRepositoryTest {


    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private AppointmentRepository repository;


    // write the test here 

}

How do I use a single class to run them all? For example, if the class will be AppointmentApplicationTests ,

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppointmentApplicationTests {

    @Test
    public void contextLoads() {

    }

}

What will be the best way to configure this class so it calls all the tests in the API and repo classes?

I think simpliest way would be to create a Suite to have a collection of tests to be run, like:

JUnit 4

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    some.package.AppointmentRepositoryTest,
    some.package.AppointmentApplicationTests
})
public class MySuite {
}

Junit5

@RunWith(JUnitPlatform.class)
@SelectClasses({some.package.AppointmentRepositoryTest,
    some.package.AppointmentAPITest.class})
public class JUnit5TestSuiteExample {
}

However this is not alwasy the best way to do it. Consider also to get acquainted with howto create test profile for maven toe perform a bunch of tests or packages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM