Difference Betwixt @Contextconfiguration Too @Springapplicationconfiguration Inward Leap Kick Integration Test

Even though both @ContextConfiguration in addition to @SpringApplicationConfiguration annotations are used along alongside SpringJUnit4ClassRunner to specify how to charge the Spring application context, at that spot is a subtle divergence betwixt them. Although @ContextConfiguration does a keen chore inwards loading application context it doesn't accept amount payoff of Spring Boot features. Spring Boot applications are ultimately loaded past times either SpringApplication ( inwards instance of the JAR) or SpringBootServletInitializer. This course of report non exclusively loads the application context but likewise enables logging in addition to loading of external properties specified inwards application.properties or application.yml file, in addition to other features of Spring Boot framework, which is non loaded or enabled past times the @ContextConfiguration annotation. 

In short, it's amend to use the @SpringApplicatoinConfiguration annotations rather than @ContextConfiguration for writing an integration examine for Spring kicking application, including a examine for spider web pages or front end destination of the application.

Now that y'all know the essential difference betwixt @ContextConfiguration in addition to @SpringApplicationConfiguration annotations spell writing Spring kicking tests, let's sympathise that inwards a lilliputian fleck to a greater extent than details alongside about code samples.

Spring Framework has first-class back upward for writing Integration examine since Spring 2.5 when it introduced SpringJUnit4ClassRunner in addition to the same goes for testing alongside Spring Boot application. During the Integration test, y'all likewise request to charge beans in addition to wire them upward alongside dependency.

Of course, y'all tin create that manually but it's amend if Spring handles them for you, which it does. It likewise offers out-of-the-box features such equally portion scanning, autowiring, proclamation transaction management, security, in addition to caching, which comes handy for testing inwards a to a greater extent than production-like environment. Btw, if y'all are non familiar alongside these essential Spring features therefore I advise y'all 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 elementary Spring integration examine alongside SpringJUnit4ClassRunner in addition to @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 y'all tin see, the BookServiceTest course of report is annotated alongside both the @RunWith JUnit annotation in addition to @ContextConfiguration annotations.

The @RunWith annotation is required to enable Spring integration testing in addition to that's why nosotros get got passed the SpringJUnit4ClassRunnner course of report to it, spell @ContextConfiguration annotation specifies how to charge application context.

In this example, the Spring application context defined inwards the BookConfiguration course of report volition hold upward loaded.

The SpringJUnit4ClassRunner is a powerful course of report which non exclusively loads the application context but likewise autowire beans into the examine equally well.

For example, inwards this examine class, nosotros needed a BookService to examine its findByTitle() method in addition to that edible bean is automatically injected past times SpringJUnit4ClassRunner course of report therefore nosotros simply left alongside writing our testBookService() method.

If y'all are non familiar alongside auto-wiring You tin see Spring Boot Reference Guide
5 Free Spring Framework in addition to Spring Boot Courses
15 Spring Boot Interview Questions for Java Developers
Top v Spring Cloud Annotations for Java Developers
Spring Boot: Efficient Development, Configuration, in addition to Deployment

Thanks for reading this article therefore far. If y'all similar my explanation of @SpringApplicationConfiguration vs @ContextConfiguration annotation therefore delight part alongside your friends in addition to colleagues. If y'all get got whatsoever questions or feedback therefore delight drib a note.

P. S. - If y'all desire to larn Spring v in addition to Spring Boot ii from scratch, inwards a guided, code-focused agency therefore I advise y'all accept a hold back at the Learn Spring: The Master Class course of report past times Eugen Paraschive of Baeldung. 

Belum ada Komentar untuk "Difference Betwixt @Contextconfiguration Too @Springapplicationconfiguration Inward Leap Kick Integration Test"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel