2013-12-16 4 views
9

मैं एक अच्छी तरह से निर्दिष्ट इंटरफेस है और उस के खिलाफ मैं अपने 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); 
    } 

[...] 

} 

तो: मेरी वर्तमान दृष्टिकोण एक संभावित अपवाद के इस तरह इंटरफ़ेस में निर्दिष्ट के लिए एक परीक्षा पद्धति में लिखने के लिए है अजीब। क्या मैं इन सभी अपवाद परीक्षणों को एक परीक्षण विधि में जोड़ सकता हूं या सबसे अच्छा अभ्यास क्या है?

उत्तर

4

दुर्भाग्य से, @Test एनोटेशन कई प्रकार के अपवाद (एपीआई संदर्भ http://junit.sourceforge.net/javadoc/org/junit/Test.html) को पकड़ने के लिए अनुमति नहीं है हो सकता था।

पहले विकल्प के रूप में, मैं टेस्टएनजी में जाने का समर्थन करता हूं। अगर आपकी टीम इसकी अनुमति नहीं देगी, तो जुनीट में आप कुछ चीजें कर सकते हैं।

निश्चित रूप से पैरामीटरयुक्त परीक्षण मामलों का उपयोग करें ताकि आपको प्रति परीक्षण केस (http://junit.sourceforge.net/javadoc/org/junit/runners/Parameterized.html) पर एक टेस्ट फ़ंक्शन लिखना न पड़े। यहां से कुछ विकल्प हैं।

  1. अपवाद प्रकारों द्वारा अपना परीक्षण डेटा समूहित करें।

    @Test (expected = IllegalArgumentException.class) 
    public void testIllegalArgumentException(String username, String message, String topic) {} 
    
    @Test (expected = NullPointerException.class) 
    public void testNullPointerException(String username, String message, String topic) {} 
    
  2. अपने विधि हस्ताक्षर में अपवाद प्रकारों को संयोजित करें। (यह मैं क्या सलाह देते है) नीचे असहज रूपरेखा ...

    public void testException(String username, String message, String topic, Class<? extends Exception>[] expectedExceptionClasses) { 
        try { 
         // exception throwing code 
        } catch (Exception e) { 
         boolean found = false; 
         for (Class<?> expectedException : expectedExceptions) { 
          if (e instanceof expectedException) { 
           found = true; 
          } 
         } 
         if (found) { 
          return; 
         } 
        } 
        Assert.fail(); 
    } 
    
  3. छाता अपवाद वर्ग के तहत अपने परीक्षण के सभी रखो (मुझे लगता है आप ऐसा नहीं करना चाहती है।)।

    @Test (expected = Exception.class) 
    public void testException(String username, String message, String topic) {} 
    
+1

यह अच्छा है, लेकिन आप ExpectedException नियम का उपयोग कर इस सुधार कर सकते हैं: https://github.com/junit-team/junit/blob/master/src/main/java/org/junit /rules/ExpectedException.java –

0

शायद उन्हें एक विधि में गठबंधन करने का सबसे अच्छा विचार नहीं हो सकता है, क्योंकि आप वास्तव में नहीं जानते कि कौन सा परीक्षण केस किस अपवाद को फेंक देता है।

उदाहरण के लिए

, यदि आप लाइन

messageService.createMessage(username, null, topic); 

जो एक NullPointerException फेंक चाहिए था, लेकिन इसके बजाय यह एक IllegalArgumentException फेंक दिया, आपको लगता है कि एक सफलता के रूप में गिनती करने के लिए नहीं करना चाहती।

यदि आप एक परीक्षण मामले में उस विधि के सभी अपवादों का परीक्षण करना चाहते हैं, तो एक अच्छा विकल्प प्रत्येक अपवाद परीक्षण को कोशिश करने के लिए होगा .. कैच ब्लॉक।

उदाहरण के लिए, आप

@Test 
public void testCreateMessageExceptions() { 
    // test #1: a null message 
    try { 
     messageService.createMessage(username, null, topic); 
     // if it got this far, that's a problem! 
     fail(); 
    } catch(NullPointerException e) { 
     // great, that's what it's meant to do! continue testing 
    } catch(Exception e) { 
     // if it threw the wrong type of exception, that's a problem! 
     fail(); 
    } 

    // test #2: an empty user 
    try { 
     messageService.createMessage("", message, topic); 
     fail(); 
    } catch(IllegalArgumentException e) { 

    } catch(Exception e) { 
     fail(); 
    } 

    // ... 
} 
संबंधित मुद्दे

 संबंधित मुद्दे