{"id":5445,"date":"2025-02-16T14:43:46","date_gmt":"2025-02-16T14:43:46","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5445"},"modified":"2025-02-16T14:57:36","modified_gmt":"2025-02-16T14:57:36","slug":"junit-faqs","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/junit-faqs\/","title":{"rendered":"JUnit FAQs"},"content":{"rendered":"<ol>\n<li><strong class=\"ctaHeader3\">Is it right to use @SpringBootTest (or) @ContextConfiguration in Unit Test?<\/strong><br \/> Starting spring for each test is a very expensive operation. It&#8217;s not a unit test anymore rather integration test. <strong>@SpringBootTest<\/strong> goes further and tries to mimic the processes added by Spring Boot framework for creating the context: Decides what to scan based on package structures, loads external configurations from predefined locations optionally runs autoconfiguration starters and so on and so forth. @SpringBootTest loads the necessary beans and inject into each other.<br \/>\nWhen using  <strong>@ContextConfiguration<\/strong> you present way to filter what exactly should be run, what beans to load and to inject into each other.\n<\/li>\n<li><strong  class=\"ctaHeader3\">How to Optimize the Unit test by selectively loading the resource?<\/strong><br \/>\nYou can selectively load the resource during unit test<\/p>\n<ul>\n<li>To test your Respository : use @DataJpaTest annotation for test slice.<\/li>\n<li>To test your Service layer : use JUnit and Mockito. Here you will mock your Repository<\/li>\n<li>To test your Controller layer : use @WebMvcTest annotation for test slice or use JUnit and Mockito. Here you will mock your Service in both cases<\/li>\n<li>To test a Component, such as a third party library wrapper or load some specific beans: use @ExtendWith(SpringExtension.class) and @ContextConfiguration\/@Import or @SpringJUnitWebConfig which is the combinaison of the both.<\/li>\n<li>To do an integration test : use @SpringBootTest<\/li>\n<\/ul>\n<\/li>\n<li><strong class=\"ctaHeader3\">When to use @SpringBootTest and  @ExtendWith(SpringExtension.class)<\/strong><br \/>\nUse <strong>@ExtendWith(SpringExtension.class)<\/strong> without loading the entire application context that is included with <strong>@SpringBootTest<\/strong>. As you say it&#8217;s a lighter weight approach if you just want to mock beans with @MockBean. @SpringBootTest does in fact include <strong>@ExtendWith(SpringExtension.class)<\/strong><\/p>\n<p>Use @SpringBootTest when,<\/p>\n<ul>\n<li>You need to test the application as a whole, including multiple layers like the web layer, service layer, and repository layer.<\/li>\n<li>You require an embedded server to run the test, such as when testing web controllers.<\/li>\n<li>You want to verify the integration of all components in a fully loaded application context.<\/li>\n<\/ul>\n<p>Use SpringRunner\/@ExtendWith(SpringExtension.class) when,<\/p>\n<ul>\n<li>You are writing unit tests or lightweight integration tests that only need certain features of Spring, such as dependency injection or transaction management, without loading the full application context.<\/li>\n<li>You want to test specific layers (e.g., service layer) in isolation without the overhead of starting the entire Spring application.<\/li>\n<\/ul>\n<\/li>\n<li><strong class=\"ctaHeader3\"><\/strong><\/li>\n<li><strong class=\"ctaHeader3\"><\/strong><\/li>\n<li><strong class=\"ctaHeader3\"><\/strong><\/li>\n<li><strong class=\"ctaHeader3\"><\/strong><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Is it right to use @SpringBootTest (or) @ContextConfiguration in Unit Test? Starting spring for each test is a very expensive operation. It&#8217;s not a unit test anymore rather integration test. @SpringBootTest goes further and tries to mimic the processes added by Spring Boot framework for creating the context: Decides what to scan based on package&hellip; <a href=\"https:\/\/codethataint.com\/blog\/junit-faqs\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[331],"tags":[],"class_list":["post-5445","post","type-post","status-publish","format-standard","hentry","category-junit5"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5445"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5445\/revisions"}],"predecessor-version":[{"id":5451,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5445\/revisions\/5451"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}