मैं एक अच्छी तरह से निर्दिष्ट इंटरफेस है और उस के खिलाफ मैं अपने JUnit परीक्षण लिखें:क्या मैं एक परीक्षण विधि में एकाधिक फेंकने वाले अपवादों का परीक्षण कर सकता हूं?
public interface ShortMessageService {
/**
* Creates a message. A message is related to a topic
* Creates a date for the message
* @throws IllegalArgumentException, if the message is longer then 255 characters.
* @throws IllegalArgumentException, if the message ist shorter then 10 characters.
* @throws IllegalArgumentException, if the user doesn't exist
* @throws IllegalArgumentException, if the topic doesn't exist
* @throws NullPointerException, if one argument is null.
* @param userName
* @param message
* @return ID of the new created message
*/
Long createMessage(String userName, String message, String topic);
[...]
}
आप देख सकते हैं कार्यान्वयन जिसके लिए मैं परीक्षण लिखने की विभिन्न अपवाद फेंक कर सकते हैं। मुझे लगता है कि एक विधि इंटरफ़ेस में परिभाषित के लिए परीक्षण तरीकों का एक बहुत परिभाषित करने के लिए है और वह महसूस करता है अब के लिए
public abstract class AbstractShortMessageServiceTest
{
String message;
String username;
String topic;
/**
* @return A new empty instance of an implementation of ShortMessageService.
*/
protected abstract ShortMessageService getNewShortMessageService();
private ShortMessageService messageService;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() throws Exception
{
messageService = getNewShortMessageService();
message = "Test Message";
username = "TestUser";
topic = "TestTopic";
}
@Test
public void testCreateMessage()
{
assertEquals(new Long(1L), messageService.createMessage(username, message, topic));
}
@Test (expected = IllegalArgumentException.class)
public void testCreateMessageUserMissing() throws Exception
{
messageService.createMessage("", message, topic);
}
@Test (expected = IllegalArgumentException.class)
public void testCreateMessageTopicMissing() throws Exception
{
messageService.createMessage(username, message, "");
}
@Test (expected = IllegalArgumentException.class)
public void testCreateMessageTooLong() throws Exception
{
String message = "";
for (int i=0; i<255; i++) {
message += "a";
}
messageService.createMessage(username, message, topic);
}
@Test (expected = IllegalArgumentException.class)
public void testCreateMessageTooShort() throws Exception
{
messageService.createMessage(username, "", topic);
}
@Test (expected = NullPointerException.class)
public void testCreateMessageNull() throws Exception
{
messageService.createMessage(username, null, topic);
}
[...]
}
तो: मेरी वर्तमान दृष्टिकोण एक संभावित अपवाद के इस तरह इंटरफ़ेस में निर्दिष्ट के लिए एक परीक्षा पद्धति में लिखने के लिए है अजीब। क्या मैं इन सभी अपवाद परीक्षणों को एक परीक्षण विधि में जोड़ सकता हूं या सबसे अच्छा अभ्यास क्या है?
यह अच्छा है, लेकिन आप ExpectedException नियम का उपयोग कर इस सुधार कर सकते हैं: https://github.com/junit-team/junit/blob/master/src/main/java/org/junit /rules/ExpectedException.java –