Difference Betwixt @Contextconfiguration In Addition To @Springapplicationconfiguration Inwards Saltation Kick Integration Test
Even though both @ContextConfiguration as well as @SpringApplicationConfiguration annotations are used along amongst SpringJUnit4ClassRunner to specify how to charge the Spring application context, at that spot is a subtle divergence betwixt them. Although @ContextConfiguration does a peachy project inwards loading application context it doesn't create got amount wages of Spring Boot features. Spring Boot applications are ultimately loaded past times either SpringApplication ( inwards illustration of the JAR) or SpringBootServletInitializer. This class non exclusively loads the application context but equally good enables logging as well as loading of external properties specified inwards application.properties or application.yml file, as well as other features of Spring Boot framework, which is non loaded or enabled past times the @ContextConfiguration annotation.
In short, it's improve to use the @SpringApplicatoinConfiguration annotations rather than @ContextConfiguration for writing an integration seek for Spring kicking application, including a seek for spider web pages or forepart halt of the application.
Now that you lot know the essential difference betwixt @ContextConfiguration as well as @SpringApplicationConfiguration annotations spell writing Spring kicking tests, let's sympathise that inwards a petty fleck to a greater extent than details amongst to a greater extent than or less code samples.
Spring Framework has fantabulous back upwards for writing Integration seek since Spring 2.5 when it introduced SpringJUnit4ClassRunner as well as the same goes for testing amongst Spring Boot application. During the Integration test, you lot equally good demand to charge beans as well as wire them upwards amongst dependency.
Of course, you lot tin create that manually but it's improve if Spring handles them for you, which it does. It equally good offers out-of-the-box features such equally constituent scanning, autowiring, proclamation transaction management, security, as well as caching, which comes handy for testing inwards a to a greater extent than production-like environment. Btw, if you lot are non familiar amongst these essential Spring features as well as therefore I advise you lot starting fourth dimension with Spring Framework 5: Beginner to Guru class on Udemy, which covers them quite well.
As you lot tin see, the BookServiceTest class is annotated amongst both the @RunWith JUnit annotation as well as @ContextConfiguration annotations.
The @RunWith annotation is required to enable Spring integration testing as well as that's why nosotros create got passed the SpringJUnit4ClassRunnner class to it, spell @ContextConfiguration annotation specifies how to charge application context.
In this example, the Spring application context defined inwards the BookConfiguration class volition hold upwards loaded.
The SpringJUnit4ClassRunner is a powerful class which non exclusively loads the application context but equally good autowire beans into the seek equally well.
For example, inwards this seek class, nosotros needed a BookService to seek its findByTitle() method as well as that edible bean is automatically injected past times SpringJUnit4ClassRunner class therefore nosotros only left amongst writing our testBookService() method.
If you lot are non familiar amongst auto-wiring You tin see Spring Boot Reference Guide
5 Free Spring Framework as well as Spring Boot Courses
15 Spring Boot Interview Questions for Java Developers
Top five Spring Cloud Annotations for Java Developers
Spring Boot: Efficient Development, Configuration, as well as Deployment
Thanks for reading this article therefore far. If you lot similar my explanation of @SpringApplicationConfiguration vs @ContextConfiguration annotation as well as therefore delight part amongst your friends as well as colleagues. If you lot create got whatever questions or feedback as well as therefore delight drib a note.
P. S. - If you lot desire to larn Spring five as well as Spring Boot two from scratch, inwards a guided, code-focused agency as well as therefore I advise you lot create got a await at the Learn Spring: The Master Class class past times Eugen Paraschive of Baeldung.
In short, it's improve to use the @SpringApplicatoinConfiguration annotations rather than @ContextConfiguration for writing an integration seek for Spring kicking application, including a seek for spider web pages or forepart halt of the application.
Now that you lot know the essential difference betwixt @ContextConfiguration as well as @SpringApplicationConfiguration annotations spell writing Spring kicking tests, let's sympathise that inwards a petty fleck to a greater extent than details amongst to a greater extent than or less code samples.
Spring Framework has fantabulous back upwards for writing Integration seek since Spring 2.5 when it introduced SpringJUnit4ClassRunner as well as the same goes for testing amongst Spring Boot application. During the Integration test, you lot equally good demand to charge beans as well as wire them upwards amongst dependency.
Of course, you lot tin create that manually but it's improve if Spring handles them for you, which it does. It equally good offers out-of-the-box features such equally constituent scanning, autowiring, proclamation transaction management, security, as well as caching, which comes handy for testing inwards a to a greater extent than production-like environment. Btw, if you lot are non familiar amongst these essential Spring features as well as therefore I advise you lot starting fourth dimension with Spring Framework 5: Beginner to Guru class on Udemy, which covers them quite well.
Spring Boot - @ContextConfiguration example
Here is a uncomplicated Spring integration seek amongst SpringJUnit4ClassRunner as well as @ContextConfiguration annotation, ane of the essential Spring kicking annotations for loading application context:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=BookConfiguration.class) public class BookServiceTest { @Autowired private BookService bookService; @Test public void testBookService() { Book aBook = bookService.findByTitle("Spring Boot inwards Action"); assertEquals("Craig Walls", aBook.getAuthor()); assertEquals(40, aBook.getPrice()); } }
As you lot tin see, the BookServiceTest class is annotated amongst both the @RunWith JUnit annotation as well as @ContextConfiguration annotations.
The @RunWith annotation is required to enable Spring integration testing as well as that's why nosotros create got passed the SpringJUnit4ClassRunnner class to it, spell @ContextConfiguration annotation specifies how to charge application context.
In this example, the Spring application context defined inwards the BookConfiguration class volition hold upwards loaded.
The SpringJUnit4ClassRunner is a powerful class which non exclusively loads the application context but equally good autowire beans into the seek equally well.
For example, inwards this seek class, nosotros needed a BookService to seek its findByTitle() method as well as that edible bean is automatically injected past times SpringJUnit4ClassRunner class therefore nosotros only left amongst writing our testBookService() method.
If you lot are non familiar amongst auto-wiring You tin see Spring Boot Reference Guide
5 Free Spring Framework as well as Spring Boot Courses
15 Spring Boot Interview Questions for Java Developers
Top five Spring Cloud Annotations for Java Developers
Spring Boot: Efficient Development, Configuration, as well as Deployment
Thanks for reading this article therefore far. If you lot similar my explanation of @SpringApplicationConfiguration vs @ContextConfiguration annotation as well as therefore delight part amongst your friends as well as colleagues. If you lot create got whatever questions or feedback as well as therefore delight drib a note.
P. S. - If you lot desire to larn Spring five as well as Spring Boot two from scratch, inwards a guided, code-focused agency as well as therefore I advise you lot create got a await at the Learn Spring: The Master Class class past times Eugen Paraschive of Baeldung.
0 Response to "Difference Betwixt @Contextconfiguration In Addition To @Springapplicationconfiguration Inwards Saltation Kick Integration Test"
Post a Comment