spring-boot junit load test property resource for unit test
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
add a comment |
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
add a comment |
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
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
java spring unit-testing spring-boot junit
asked Nov 22 '18 at 1:26
VelNagaVelNaga
1,1221440
1,1221440
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use @TestPropertySource
See this page: override-default-spring-boot-application-properties-settings-in-junit-test
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
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You can use @TestPropertySource
See this page: override-default-spring-boot-application-properties-settings-in-junit-test
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
add a comment |
You can use @TestPropertySource
See this page: override-default-spring-boot-application-properties-settings-in-junit-test
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
add a comment |
You can use @TestPropertySource
See this page: override-default-spring-boot-application-properties-settings-in-junit-test
You can use @TestPropertySource
See this page: override-default-spring-boot-application-properties-settings-in-junit-test
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 22 '18 at 11:33
VelNagaVelNaga
1,1221440
1,1221440
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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