0
votes

I have a question about the usage of DataJpaTest annotation. I am trying to test a Jpa repository, exactly as shown in the documentation.

I am getting an error that the HttpServletRequest cannot be resolved. It is because a different bean of mine is using it.

Why is the test trying to use the irrelevant bean? I would expect a DataJpaTest to only load Jpa related beans, repositories, etc. It seems it is trying to load all beans, which of course have their own dependencies.

What is the correct way to write a DataJpaTest so that I only focus on my Jpa repositories?

This is on Java 8, Spring Boot 2 and junit 5.

Update 1: thanks for the comments guys. My test class is literally based on the documentation.

My Spring Boot application class is like this:

@SpringBootApplication
@EnableSwagger2
@EnableCorsFilter
@ComponentScan(basePackages = {"com.acme.superapp"})
@SuppressWarnings("HideUtilityClassConstructor")
public class Swagger2SpringBoot {
    public static void main(String[] args) {
        new SpringApplication(Swagger2SpringBoot.class).run(args);
    }
}

This actually helped because after I removed the "ComponentScan" annotation I get a different error, related to Swagger.

So it seems these annotations are affecting my test.

1
Show your @SpringBootApplication class.M. Deinum
Even better... The test classshinjw
Please show the test class. How should we help if we don't know what you are doing?Simon Martinelli
Creating configuration classes and slimming down the entrypoint bare minimum as possible would greatly help your causeshinjw

1 Answers

0
votes

Thanks for your comments guys. The comment from M. Deinum and shinjw showed me to the correct path. I needed to slim down the entrypoint so that it does not have anything extra.

According to the documentation :

If you structure your code in a sensible way, your @SpringBootApplication class is used by default as the configuration of your tests.

It then becomes important not to litter the application’s main class with configuration settings that are specific to a particular area of its functionality.

Therefore I moved the ComponentScan and EnableSwagger2 annotations elsewhere, in different Configuration classes and this did the trick.

Thanks for your help!