Saturday, March 18, 2023
HomeJavaDistinction in between @ContextConfiguration and also @SpringApplicationConfiguration in Springtime Boot Screening?

Distinction in between @ContextConfiguration and also @SpringApplicationConfiguration in Springtime Boot Screening?


Hey there individuals, what is the distinction in between @ContextConfiguration and also @SpringApplicationConfiguration is just one of the regularly asked Springtime Boot Evaluating meeting concerns which is typically made use of to examine if the prospect understands about just how to device examination Springtime boot application. Despite the fact that both @ContextConfiguration and also @SpringApplicationConfiguration comments are made use of together with SpringJUnit4ClassRunner to define just how to pack the Springtime application context, there is a refined distinction in between them. Although @ContextConfiguration does a terrific task in packing application context it does not make the most of Springtime Boot functions Springtime Boot applications are eventually packed by either SpringApplication (when it comes to the container) or SpringBootServletInitializer
This course not just tons the application context yet additionally makes it possible for logging and also packing of outside buildings defined in the application.properties or application.yml data, and also various other functions of the Springtime Boot structure, which is not packed or made it possible for by the @ContextConfiguration note.

In other words, it’s much better to make use of the @SpringApplicatoinConfiguration comments as opposed to @ContextConfiguration for composing a combination examination for the Springtime boot application, consisting of an examination for websites or the front end of the application.

Since you recognize the vital distinction in between @ContextConfiguration and also @SpringApplicationConfiguration comments while composing Springtime boot examinations, allow’s comprehend that in a bit even more information with some code examples.

Springtime Structure has exceptional assistance for composing the Assimilation examination given that Springtime 2.5 when it presented SpringJUnit4ClassRunner and also the very same chooses screening with the Springtime Boot application. Throughout the Assimilation examination, you additionally require to pack beans and also wire them up with dependence.

Obviously, you can do that by hand yet it’s much better if Springtime manages them for you, which it does. It additionally supplies out-of-the-box functions such as element scanning, autowiring, statement purchase monitoring, protection, and also caching, which is available in convenient for screening in a much more production-like setting. Btw, if you are not acquainted with these vital Springtime functions after that I recommend you begin with Springtime Structure 5: Novice to Expert training course on Udemy, which covers them fairly well.

Springtime Boot – @ContextConfiguration instance

Right here is an easy Springtime combination examination with SpringJUnit4ClassRunner and also @ContextConfiguration note, among the vital Springtime boot comments for packing application context:

 @RunWith( SpringJUnit4ClassRunner.class).
@ContextConfiguration( courses= BookConfiguration.class).
 public  course BookServiceTest  {


 @ Autowired.
 personal BookService bookService;


 @ Examination.
 public  space testBookService()  {.
Schedule aBook  = bookService findByTitle(" Springtime Boot at work");
assertEquals(" Craig Walls", aBook getAuthor());
assertEquals( 40, aBook getPrice());
} 

} 

As you can see, the BookServiceTest course is annotated with both the @RunWith JUnit note and also @ContextConfiguration comments.

The @RunWith note is called for to make it possible for Springtime combination screening which’s why we have actually passed the SpringJUnit4ClassRunnner course to it, while @ContextConfiguration note defines just how to pack application context.

In this instance, the Springtime application context specified in the BookConfiguration course will certainly be packed.

The SpringJUnit4ClassRunner is an effective course that not just tons the application context yet additionally autowire beans right into the examination too.

For instance, in this examination course, we required a BookService to examine its findByTitle() technique, which bean is instantly infused by SpringJUnit4ClassRunner course so we simply entrusted to composing our testBookService() technique.

If you are not acquainted with auto-wiring You can see Developing Your First Springtime Boot Application training course on Pluralsight to read more concerning autowiring and also just how Springtime boot application launches.

Difference between @ContextConfiguration and @SpringApplicationConfiguration in Spring Boot Integration Test

Though, you would certainly require a Pluralsight subscription to gain access to this training course which sets you back around $29 monthly or $299 annually. If you do not have that you can still access this training course by making the most of their 10-day complimentary test which permits accessibility to greater than 5000+ Pluralsight online training courses with no responsibility.

Currently, returning to @ContextConfiguration, although it assists to pack applicationcontext, it does not make it possible for logging or tons added buildings from the application.properties like server.port home which is called for to alter the port of ingrained tomcat web server in springtime boot application.

To resolve that issue, you can make use of @SpringApplicaitonCongifguation note instead of @ContextConfiguration as revealed listed below:

 @RunWith( SpringJUnit4ClassRunner.class).
@SpringApplicaitonCongifguation( courses= BookConfiguration.class).
 public  course BookServiceTest  {

 @ Autowired.
 personal BookService bookService;


 @ Examination.
 public  space testBookService()  {.
Schedule aBook  = bookService findByTitle(" Cloud Indigenous Java");
assertEquals(" Josh Long", aBook getAuthor());
assertEquals( 40, aBook getPrice());
} 
} 

This will certainly act the like the previous instance yet @SpringApplicationConfiguration will certainly additionally make it possible for Springtime boot logging and also tons added buildings specified in application.properties or application.yml data. This assists you to compose even more sensible and also useful device examinations.

If you are brand-new to Springtime Boot screening or wish to take your Springtime Boot screening abilities to following degree after that I very advise you to sign up with Evaluating Springtime Boot: Novice to Expert training course by John Thompson on Udemy. This training course will certainly assist you to grasp device screening in Java and also Springtime boot application with JUnit 5, Mockito, and also Springtime Boot and also assist you come to be a much better programmer.
best course to learn Spring Boot Testing for Java programmers



Previous article
RELATED ARTICLES

Most Popular

Recent Comments