简体   繁体   中英

@DataJpaTest - loads configuration from @SpringBootApplication class

I am confused a bit,based on this documentation , I should be only putting this annotation @DataJpaTest and it will configure my application context to be only for JPA repositories and that too for an in-memory database. Now the problem is in my application I have a Main class which is annotated with @SpringBootApplication and it is loading rest of the configuration for Web interceptor and many other things from there. In fact it is trying to load the bootstrap.properties file too.

In my understanding it should NOT be using this configuration.

Below is my test code and main class.

@OpenAPIDefinition(info = @Info(title = "Test API", version = "0.1.10-SNAPSHOT", description = "Test API", contact = @Contact(name = "My Team", email = "sample@mycompany.com")), servers = {
        @Server(description = "Current Stage Server url", url = "https://mycompany.com/testapics"),
        @Server(description = "Stable Stage Server url", url = "https://mycompany.com/testapi") })
@SpringBootApplication(scanBasePackages = { "com.mycompany.utility", "com.mycompany.sample.users",
        "com.mycompany.sample.utility", "com.mycompany.another.sample.interceptor",
        "com.mycompany.sample.security.vulnerableinput", "com.mycompany.sample.security.nocache.filter",
        "com.mycompany.sample.security.common", "com.mycompany.sample.security.csrf",
        "com.mycompany.sample.security.nocache" })
@EnableCaching
@EnableAsync
public class SampleApiApplication implements WebMvcConfigurer {

The main class has a bunch of other bean configurations in it. (Omitting those for clarity).

Below is my test class.

@DataJpaTest
public class UsersRepositoryTest {

    @Autowired
    private UsersRepository usersRepository;

    @Test
    public void test_usersRepository_findByEmplId() {
        List<User> users = getUsersData();
        UsersRepository.saveAll(users);
        for (User user : users) {
            Assert.assertTrue(user.getId() != null);
        }
    }

    private List<User> getUsersData() {
        List<User> userList = new ArrayList<>();
        User user = new User();
        user.setEmpId("XYZ_001");
        user.setUserUUID(UUID.randomUUID().toString());
        userList.add(user);
        return userList;
    }

}

According to the Spring Boot Reference :

If you use a test annotation to test a more specific slice of your application , you should avoid adding configuration settings that are specific to a particular area on the main method's application class .

The underlying component scan configuration of @SpringBootApplication defines exclude filters that are used to make sure slicing works as expected. If you are using an explicit @ComponentScan directive on your @SpringBootApplication -annotated class, be aware that those filters will be disabled. If you are using slicing, you should define them again.

So in your case consider to move annotations like @EnableCaching , @EnableAsync to a separate class.

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