Difference Betwixt @Contextconfiguration In Addition To @Springapplicationconfiguration Inwards Boundary Kick Integration Test
Even though both @ContextConfiguration together with @SpringApplicationConfiguration annotations are used along amongst SpringJUnit4ClassRunner to specify how to charge the Spring application context, at that spot is a subtle departure betwixt them. Although @ContextConfiguration does a neat chore inward loading application context it doesn't accept amount wages of Spring Boot features. Spring Boot applications are ultimately loaded past times either SpringApplication ( inward instance of the JAR) or SpringBootServletInitializer. This flat non exclusively loads the application context but likewise enables logging together with loading of external properties specified inward application.properties or application.yml file, together with 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 attempt for Spring kick application, including a attempt for spider web pages or forepart destination of the application.
Now that you lot know the essential difference betwixt @ContextConfiguration together with @SpringApplicationConfiguration annotations acre writing Spring kick tests, let's empathise that inward a niggling fleck to a greater extent than details amongst around code samples.
Spring Framework has first-class back upward for writing Integration attempt since Spring 2.5 when it introduced SpringJUnit4ClassRunner together with the same goes for testing amongst Spring Boot application. During the Integration test, you lot likewise require to charge beans together with wire them upward amongst dependency.
Of course, you lot tin produce that manually but it's improve if Spring handles them for you, which it does. It likewise offers out-of-the-box features such every bit element scanning, autowiring, annunciation transaction management, security, together with caching, which comes handy for testing inward a to a greater extent than production-like environment. Btw, if you lot are non familiar amongst these essential Spring features hence I propose you lot commencement with Spring Framework 5: Beginner to Guru course of report on Udemy, which covers them quite well.
As you lot tin see, the BookServiceTest flat is annotated amongst both the @RunWith JUnit annotation together with @ContextConfiguration annotations.
The @RunWith annotation is required to enable Spring integration testing together with that's why nosotros conduct maintain passed the SpringJUnit4ClassRunnner flat to it, acre @ContextConfiguration annotation specifies how to charge application context.
In this example, the Spring application context defined inward the BookConfiguration flat volition last loaded.
The SpringJUnit4ClassRunner is a powerful flat which non exclusively loads the application context but likewise autowire beans into the attempt every bit well.
For example, inward this attempt class, nosotros needed a BookService to attempt its findByTitle() method together with that edible bean is automatically injected past times SpringJUnit4ClassRunner flat hence nosotros simply 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 together with Spring Boot Courses
15 Spring Boot Interview Questions for Java Developers
Top v Spring Cloud Annotations for Java Developers
Spring Boot: Efficient Development, Configuration, together with Deployment
Thanks for reading this article hence far. If you lot similar my explanation of @SpringApplicationConfiguration vs @ContextConfiguration annotation hence delight percentage amongst your friends together with colleagues. If you lot conduct maintain whatever questions or feedback hence delight drib a note.
P. S. - If you lot desire to acquire Spring v together with Spring Boot two from scratch, inward a guided, code-focused means hence I propose you lot accept a hold back at the Learn Spring: The Master Class course of report past times Eugen Paraschive of Baeldung.
In short, it's improve to use the @SpringApplicatoinConfiguration annotations rather than @ContextConfiguration for writing an integration attempt for Spring kick application, including a attempt for spider web pages or forepart destination of the application.
Now that you lot know the essential difference betwixt @ContextConfiguration together with @SpringApplicationConfiguration annotations acre writing Spring kick tests, let's empathise that inward a niggling fleck to a greater extent than details amongst around code samples.
Spring Framework has first-class back upward for writing Integration attempt since Spring 2.5 when it introduced SpringJUnit4ClassRunner together with the same goes for testing amongst Spring Boot application. During the Integration test, you lot likewise require to charge beans together with wire them upward amongst dependency.
Of course, you lot tin produce that manually but it's improve if Spring handles them for you, which it does. It likewise offers out-of-the-box features such every bit element scanning, autowiring, annunciation transaction management, security, together with caching, which comes handy for testing inward a to a greater extent than production-like environment. Btw, if you lot are non familiar amongst these essential Spring features hence I propose you lot commencement with Spring Framework 5: Beginner to Guru course of report on Udemy, which covers them quite well.
Spring Boot - @ContextConfiguration example
Here is a uncomplicated Spring integration attempt amongst SpringJUnit4ClassRunner together with @ContextConfiguration annotation, 1 of the essential Spring kick 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 inward Action"); assertEquals("Craig Walls", aBook.getAuthor()); assertEquals(40, aBook.getPrice()); } }
As you lot tin see, the BookServiceTest flat is annotated amongst both the @RunWith JUnit annotation together with @ContextConfiguration annotations.
The @RunWith annotation is required to enable Spring integration testing together with that's why nosotros conduct maintain passed the SpringJUnit4ClassRunnner flat to it, acre @ContextConfiguration annotation specifies how to charge application context.
In this example, the Spring application context defined inward the BookConfiguration flat volition last loaded.
The SpringJUnit4ClassRunner is a powerful flat which non exclusively loads the application context but likewise autowire beans into the attempt every bit well.
For example, inward this attempt class, nosotros needed a BookService to attempt its findByTitle() method together with that edible bean is automatically injected past times SpringJUnit4ClassRunner flat hence nosotros simply 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 together with Spring Boot Courses
15 Spring Boot Interview Questions for Java Developers
Top v Spring Cloud Annotations for Java Developers
Spring Boot: Efficient Development, Configuration, together with Deployment
Thanks for reading this article hence far. If you lot similar my explanation of @SpringApplicationConfiguration vs @ContextConfiguration annotation hence delight percentage amongst your friends together with colleagues. If you lot conduct maintain whatever questions or feedback hence delight drib a note.
P. S. - If you lot desire to acquire Spring v together with Spring Boot two from scratch, inward a guided, code-focused means hence I propose you lot accept a hold back at the Learn Spring: The Master Class course of report past times Eugen Paraschive of Baeldung.
0 Response to "Difference Betwixt @Contextconfiguration In Addition To @Springapplicationconfiguration Inwards Boundary Kick Integration Test"
Post a Comment