Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine @DataJpaTest @SpringBootTest in one MVC application for testing every layer?

Here https://stackoverflow.com/a/52968130/10894456 is well explained why @DataJpaTest @SpringBootTest shouldn't be mixed in one application.

But barely explained the case when anyway need to test every layer of MVC SpringBoot application (from my point it's natural to test not only one or only another layer but both and even all layers, isn't it?)

So there was suggested a solution to use @AutoConfigureTestDatabase rather than @DataJpaTest but didn't finish the job (((

So my question is: is using @AutoConfigureTestDatabase an appropriate solution? If yes, please explain details. If no, please suggest more appropriate solution, thank you

like image 915
J.J. Beam Avatar asked Apr 24 '19 20:04

J.J. Beam


People also ask

Which of the following annotation can be used for testing spring boot application which creates the entire context without starting the server?

Spring Boot provides a @SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication .

What is DataJpaTest?

@DataJpaTest is used to test JPA repositories. It is used in combination with @RunWith(SpringRunner. class) . The annotation disables full auto-configuration and applies only configuration relevant to JPA tests. By default, tests annotated with @DataJpaTest use an embedded in-memory database.


1 Answers

Assuming you want the full application with the exception of database being in-memory, yes, combining @SpringBootTest with @AutoConfigureTestDatabase is an appropriate solution, as it does just that.

It is also mentioned in the documentation of @DataJpaTest:

If you are looking to load your full application configuration, but use an embedded database, you should consider @SpringBootTest combined with @AutoConfigureTestDatabase rather than this annotation.

like image 154
eis Avatar answered Nov 29 '22 10:11

eis