spring-boot junit load test property resource for unit test












0















I am using spring-boot-1.5. Is there any way to load application.properties in src/test/resources during the unit test? I know how to load it using integration test we can use @SpringBootTest or @ContextConfiguration but I would like to use application.properties during unit test.



Let me explain my scenario a bit,



@SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class BookBackendApplication {

public static void main(String args) {
SpringApplication.run(BookBackendApplication.class, args);
}
}

@Service
public class BookService {
@Value("${book-list:ACTIVE}")
private List<String> bookList = new ArrayList<>();
@Value("${book-status:PURCHASING}")
private String bookStatus;

public BookResponse purchaseBook(BookRequest bookRequest) {
if(bookRequest.getStatus().equals(bookStatus)) { //Here getting NPE while executing unit test
....
}
}
}


UNIT TEST



@RunWith(SpringRunner.class)
public class BookServiceTest {

@InjectMocks
private BookService bookService;

@Test
public void testBookActivate() throws IOException {
BookResponse bookResponse = bookService.purchaseBook(bookRequest);
...
}
}


Properties are not loading while running this unit test so getting NPE in BookService.java. Is there any way to load src/test/resource/application.properties while running the unit test?



Any pointers or help would be really appreciable.










share|improve this question



























    0















    I am using spring-boot-1.5. Is there any way to load application.properties in src/test/resources during the unit test? I know how to load it using integration test we can use @SpringBootTest or @ContextConfiguration but I would like to use application.properties during unit test.



    Let me explain my scenario a bit,



    @SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
    @EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
    public class BookBackendApplication {

    public static void main(String args) {
    SpringApplication.run(BookBackendApplication.class, args);
    }
    }

    @Service
    public class BookService {
    @Value("${book-list:ACTIVE}")
    private List<String> bookList = new ArrayList<>();
    @Value("${book-status:PURCHASING}")
    private String bookStatus;

    public BookResponse purchaseBook(BookRequest bookRequest) {
    if(bookRequest.getStatus().equals(bookStatus)) { //Here getting NPE while executing unit test
    ....
    }
    }
    }


    UNIT TEST



    @RunWith(SpringRunner.class)
    public class BookServiceTest {

    @InjectMocks
    private BookService bookService;

    @Test
    public void testBookActivate() throws IOException {
    BookResponse bookResponse = bookService.purchaseBook(bookRequest);
    ...
    }
    }


    Properties are not loading while running this unit test so getting NPE in BookService.java. Is there any way to load src/test/resource/application.properties while running the unit test?



    Any pointers or help would be really appreciable.










    share|improve this question

























      0












      0








      0








      I am using spring-boot-1.5. Is there any way to load application.properties in src/test/resources during the unit test? I know how to load it using integration test we can use @SpringBootTest or @ContextConfiguration but I would like to use application.properties during unit test.



      Let me explain my scenario a bit,



      @SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
      @EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
      public class BookBackendApplication {

      public static void main(String args) {
      SpringApplication.run(BookBackendApplication.class, args);
      }
      }

      @Service
      public class BookService {
      @Value("${book-list:ACTIVE}")
      private List<String> bookList = new ArrayList<>();
      @Value("${book-status:PURCHASING}")
      private String bookStatus;

      public BookResponse purchaseBook(BookRequest bookRequest) {
      if(bookRequest.getStatus().equals(bookStatus)) { //Here getting NPE while executing unit test
      ....
      }
      }
      }


      UNIT TEST



      @RunWith(SpringRunner.class)
      public class BookServiceTest {

      @InjectMocks
      private BookService bookService;

      @Test
      public void testBookActivate() throws IOException {
      BookResponse bookResponse = bookService.purchaseBook(bookRequest);
      ...
      }
      }


      Properties are not loading while running this unit test so getting NPE in BookService.java. Is there any way to load src/test/resource/application.properties while running the unit test?



      Any pointers or help would be really appreciable.










      share|improve this question














      I am using spring-boot-1.5. Is there any way to load application.properties in src/test/resources during the unit test? I know how to load it using integration test we can use @SpringBootTest or @ContextConfiguration but I would like to use application.properties during unit test.



      Let me explain my scenario a bit,



      @SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
      @EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
      public class BookBackendApplication {

      public static void main(String args) {
      SpringApplication.run(BookBackendApplication.class, args);
      }
      }

      @Service
      public class BookService {
      @Value("${book-list:ACTIVE}")
      private List<String> bookList = new ArrayList<>();
      @Value("${book-status:PURCHASING}")
      private String bookStatus;

      public BookResponse purchaseBook(BookRequest bookRequest) {
      if(bookRequest.getStatus().equals(bookStatus)) { //Here getting NPE while executing unit test
      ....
      }
      }
      }


      UNIT TEST



      @RunWith(SpringRunner.class)
      public class BookServiceTest {

      @InjectMocks
      private BookService bookService;

      @Test
      public void testBookActivate() throws IOException {
      BookResponse bookResponse = bookService.purchaseBook(bookRequest);
      ...
      }
      }


      Properties are not loading while running this unit test so getting NPE in BookService.java. Is there any way to load src/test/resource/application.properties while running the unit test?



      Any pointers or help would be really appreciable.







      java spring unit-testing spring-boot junit






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 1:26









      VelNagaVelNaga

      1,1221440




      1,1221440
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can use @TestPropertySource



          See this page: override-default-spring-boot-application-properties-settings-in-junit-test






          share|improve this answer
























          • Nope it didn’t work ....I’ve already tried it ....it will work only with context configuration...for unit test you don’t require context ....Same applicable for propertyResource

            – VelNaga
            Nov 22 '18 at 1:49



















          0














          Finally, I got an answer. I have used below code to set static fields while running mocks. ReflectionTestUtils resolved the issue.



          @RunWith(SpringRunner.class)
          public class BookServiceTest {

          @InjectMocks
          private BookService bookService;

          @Before
          public void setup() {
          ReflectionTestUtils.setField(bookService, "bookStatus", "PURCHASING");
          ReflectionTestUtils.setField(bookService, "bookList", Arrays.asList("ACTIVE","ACTIVATING"));
          }

          @Test
          public void testBookActivate() throws IOException {
          BookResponse bookResponse = bookService.purchaseBook(bookRequest);
          ...
          }
          }


          Thanks a lot for the support guys. Let's keep helping others.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422676%2fspring-boot-junit-load-test-property-resource-for-unit-test%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can use @TestPropertySource



            See this page: override-default-spring-boot-application-properties-settings-in-junit-test






            share|improve this answer
























            • Nope it didn’t work ....I’ve already tried it ....it will work only with context configuration...for unit test you don’t require context ....Same applicable for propertyResource

              – VelNaga
              Nov 22 '18 at 1:49
















            0














            You can use @TestPropertySource



            See this page: override-default-spring-boot-application-properties-settings-in-junit-test






            share|improve this answer
























            • Nope it didn’t work ....I’ve already tried it ....it will work only with context configuration...for unit test you don’t require context ....Same applicable for propertyResource

              – VelNaga
              Nov 22 '18 at 1:49














            0












            0








            0







            You can use @TestPropertySource



            See this page: override-default-spring-boot-application-properties-settings-in-junit-test






            share|improve this answer













            You can use @TestPropertySource



            See this page: override-default-spring-boot-application-properties-settings-in-junit-test







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 1:47









            applemangoapplemango

            567




            567













            • Nope it didn’t work ....I’ve already tried it ....it will work only with context configuration...for unit test you don’t require context ....Same applicable for propertyResource

              – VelNaga
              Nov 22 '18 at 1:49



















            • Nope it didn’t work ....I’ve already tried it ....it will work only with context configuration...for unit test you don’t require context ....Same applicable for propertyResource

              – VelNaga
              Nov 22 '18 at 1:49

















            Nope it didn’t work ....I’ve already tried it ....it will work only with context configuration...for unit test you don’t require context ....Same applicable for propertyResource

            – VelNaga
            Nov 22 '18 at 1:49





            Nope it didn’t work ....I’ve already tried it ....it will work only with context configuration...for unit test you don’t require context ....Same applicable for propertyResource

            – VelNaga
            Nov 22 '18 at 1:49













            0














            Finally, I got an answer. I have used below code to set static fields while running mocks. ReflectionTestUtils resolved the issue.



            @RunWith(SpringRunner.class)
            public class BookServiceTest {

            @InjectMocks
            private BookService bookService;

            @Before
            public void setup() {
            ReflectionTestUtils.setField(bookService, "bookStatus", "PURCHASING");
            ReflectionTestUtils.setField(bookService, "bookList", Arrays.asList("ACTIVE","ACTIVATING"));
            }

            @Test
            public void testBookActivate() throws IOException {
            BookResponse bookResponse = bookService.purchaseBook(bookRequest);
            ...
            }
            }


            Thanks a lot for the support guys. Let's keep helping others.






            share|improve this answer




























              0














              Finally, I got an answer. I have used below code to set static fields while running mocks. ReflectionTestUtils resolved the issue.



              @RunWith(SpringRunner.class)
              public class BookServiceTest {

              @InjectMocks
              private BookService bookService;

              @Before
              public void setup() {
              ReflectionTestUtils.setField(bookService, "bookStatus", "PURCHASING");
              ReflectionTestUtils.setField(bookService, "bookList", Arrays.asList("ACTIVE","ACTIVATING"));
              }

              @Test
              public void testBookActivate() throws IOException {
              BookResponse bookResponse = bookService.purchaseBook(bookRequest);
              ...
              }
              }


              Thanks a lot for the support guys. Let's keep helping others.






              share|improve this answer


























                0












                0








                0







                Finally, I got an answer. I have used below code to set static fields while running mocks. ReflectionTestUtils resolved the issue.



                @RunWith(SpringRunner.class)
                public class BookServiceTest {

                @InjectMocks
                private BookService bookService;

                @Before
                public void setup() {
                ReflectionTestUtils.setField(bookService, "bookStatus", "PURCHASING");
                ReflectionTestUtils.setField(bookService, "bookList", Arrays.asList("ACTIVE","ACTIVATING"));
                }

                @Test
                public void testBookActivate() throws IOException {
                BookResponse bookResponse = bookService.purchaseBook(bookRequest);
                ...
                }
                }


                Thanks a lot for the support guys. Let's keep helping others.






                share|improve this answer













                Finally, I got an answer. I have used below code to set static fields while running mocks. ReflectionTestUtils resolved the issue.



                @RunWith(SpringRunner.class)
                public class BookServiceTest {

                @InjectMocks
                private BookService bookService;

                @Before
                public void setup() {
                ReflectionTestUtils.setField(bookService, "bookStatus", "PURCHASING");
                ReflectionTestUtils.setField(bookService, "bookList", Arrays.asList("ACTIVE","ACTIVATING"));
                }

                @Test
                public void testBookActivate() throws IOException {
                BookResponse bookResponse = bookService.purchaseBook(bookRequest);
                ...
                }
                }


                Thanks a lot for the support guys. Let's keep helping others.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 11:33









                VelNagaVelNaga

                1,1221440




                1,1221440






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422676%2fspring-boot-junit-load-test-property-resource-for-unit-test%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen