Friday, March 24, 2023
HomeJavaDistinction between @ContextConfiguration and @SpringApplicationConfiguration in Spring Boot Testing?

Distinction between @ContextConfiguration and @SpringApplicationConfiguration in Spring Boot Testing?


Hi there people, what’s the distinction between @ContextConfiguration and @SpringApplicationConfiguration is among the steadily requested Spring Boot Testing interview questions which is commonly used to test if the candidate is aware of about how one can unit check Spring boot utility. Although each @ContextConfiguration and @SpringApplicationConfiguration annotations are used together with SpringJUnit4ClassRunner to specify how one can load the Spring utility context, there’s a delicate distinction between them. Though @ContextConfiguration does an excellent job in loading utility context it does not take full benefit of Spring Boot options. Spring Boot purposes are in the end loaded by both SpringApplication ( within the case of the JAR) or SpringBootServletInitializer
This class not solely hundreds the appliance context but in addition allows logging and loading of exterior properties specified within the utility.properties or utility.yml file, and different options of the Spring Boot framework, which isn’t loaded or enabled by the @ContextConfiguration annotation. 

Briefly, it is higher to use the @SpringApplicatoinConfiguration annotations slightly than @ContextConfiguration for writing an integration check for the Spring boot utility, together with a check for internet pages or the entrance finish of the appliance.

Now that the important distinction between @ContextConfiguration and @SpringApplicationConfiguration annotations whereas writing Spring boot assessments, let’s perceive that in a bit bit extra particulars with some code samples.

Spring Framework has glorious help for writing the Integration check since Spring 2.5 when it launched SpringJUnit4ClassRunner and the identical goes for testing with the Spring Boot utility. Through the Integration check, you additionally have to load beans and wire them up with dependency.

After all, you are able to do that manually but it surely’s higher if Spring handles them for you, which it does. It additionally affords out-of-the-box options corresponding to part scanning, autowiring, declaration transaction administration, safety, and caching, which turns out to be useful for testing in a extra production-like atmosphere. Btw, in case you are not aware of these important Spring options then I counsel you begin with Spring Framework 5: Newbie to Guru course on Udemy, which covers them fairly nicely.

Spring Boot – @ContextConfiguration instance

Right here is a straightforward Spring integration check with SpringJUnit4ClassRunner and @ContextConfiguration annotation, one of many important Spring boot annotations for loading utility context:

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


@Autowired
non-public BookService bookService;


@Check
public void testBookService() {
  E-book aBook = bookService.findByTitle("Spring Boot in Motion");
  assertEquals("Craig Partitions", aBook.getAuthor());
  assertEquals(40, aBook.getPrice());
}

}

As you possibly can see, the BookServiceTest class is annotated with each the @RunWith JUnit annotation and @ContextConfiguration annotations.

The @RunWith annotation is required to allow Spring integration testing and that is why we now have handed the SpringJUnit4ClassRunnner class to it, whereas @ContextConfiguration annotation specifies how one can load utility context.

On this instance, the Spring utility context outlined within the BookConfiguration class might be loaded.

The SpringJUnit4ClassRunner is a robust class that not solely hundreds the appliance context but in addition autowire beans into the check as nicely.

For instance, on this check class, we would have liked a BookService to check its findByTitle() technique, and that bean is robotically injected by SpringJUnit4ClassRunner class so we simply left with writing our testBookService() technique.

In case you are not aware of auto-wiring You’ll be able to see Creating Your First Spring Boot Software course on Pluralsight to study extra about autowiring and the way Spring boot utility begins up.

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

Although, you would want a Pluralsight membership to entry this course which prices round $29 per 30 days or $299 per yr. If you do not have you can nonetheless entry this course by profiting from their 10-day free trial which permits entry to greater than 5000+ Pluralsight on-line programs with none obligation.

Now, coming again to @ContextConfiguration, although it helps to load applicationcontext, it does not allow logging or hundreds extra properties from the utility.properties like server.port property which is required to alter the port of embedded tomcat server in spring boot utility.

To resolve that drawback, you should utilize @SpringApplicaitonCongifguation annotation instead of @ContextConfiguration as proven beneath:

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

@Autowired
non-public BookService bookService;


@Check
public void testBookService() {
  E-book aBook = bookService.findByTitle("Cloud Native Java");
  assertEquals("Josh Lengthy", aBook.getAuthor());
  assertEquals(40, aBook.getPrice());
}
}

This one will behave the identical because the earlier instance however @SpringApplicationConfiguration may also allow Spring boot logging and cargo extra properties outlined in utility.properties or utility.yml file. This lets you write extra life like and sensible unit assessments.  

In case you are new to Spring Boot testing or wish to take your Spring Boot testing abilities to subsequent degree then I extremely advocate you to hitch Testing Spring Boot: Newbie to Guru course by John Thompson on Udemy. This course will show you how to to grasp unit testing in Java and Spring boot utility with JUnit 5, Mockito, and Spring Boot and show you how to turn out to be a greater developer. 
best course to learn Spring Boot Testing for Java programmers



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments