Reputation: 1738
I'm quite new to jUnit
testing and I'm trying to write some integration test
for my Spring Boot
application. My plan is to test, whether all mandatory attributes of an object are set. I came up with something like:
@Test(expected = org.springframework.orm.jpa.JpaSystemException.class)
public void testMessageMandatoryAttributes() {
Message corruptedMessage = new Message();
// set id
// corruptedMessage.setId(id);
// set conversation thread
// corruptedMessage.setConversationThread(conversationThread);
messageRepository.save(corruptedMessage);
}
Nevertheless my Message entity has more mandatory attributes...how to test in just one function that all of them are properly set?
Upvotes: 1
Views: 1208
Reputation: 8230
Basically you need to test that messageRepository.save(Message)
method throws an exception containing some information about missing fields.
Find below a code snippet that may help you to achieve your goal. Replace the assertion in the catch-block with whatever you need to verify.
@Test
public void testMessageMandatoryAttributes() {
Message corruptedMessage = new Message();
// set id
// corruptedMessage.setId(id);
// set conversation thread
// corruptedMessage.setConversationThread(conversationThread);
try {
messageRepository.save(corruptedMessage);
fail();
catch (YourException e) {
assertEquals("Expected value", e.getXxx());
// ...
}
}
Upvotes: 1
Reputation: 71
If you want to assert exception then I would suggest using ExpectedException
. If you want to verify object properties then I would suggest to use write you custom matcher.
Upvotes: 0