2012-04-13 204 views
6

如何使用的assertEquals,看是否異常消息是正確的? 測試通過,但我不知道它是否符合正確的錯誤。的JUnit測試 - 的assertEquals異常

我正在運行的測試。

@Test 
public void testTC3() 
{ 
    try { 
    assertEquals("Legal Values: Package Type must be P or R", Shipping.shippingCost('P', -5)); 
    } 
    catch (Exception e) { 
    }   
} 

的方法進行測試。

public static int shippingCost(char packageType, int weight) throws Exception 
{ 
    String e1 = "Legal Values: Package Type must be P or R"; 
    String e2 = "Legal Values: Weight < 0"; 
    int cost = 0; 
     if((packageType != 'P')&&(packageType != 'R')) 
     { 
      throw new Exception(e1); 
     } 

     if(weight < 0) 
     { 
      throw new Exception(e2); 
     }   
     if(packageType == 'P') 
     { 
      cost += 10; 
     } 
     if(weight <= 25) 
     { 
      cost += 10; 
     } 
     else 
     { 
      cost += 25; 
     } 
     return cost;  
} 

}

感謝您的幫助。

回答

6
try { 
    assertEquals("Legal Values: Package Type must be P or R", Shipping.shippingCost('P', -5)); 
    Assert.fail("Should have thrown an exception"); 
} 
catch (Exception e) { 
    String expectedMessage = "this is the message I expect to get"; 
    Assert.assertEquals("Exception message must be correct", expectedMessage, e.getMessage()); 
} 
+1

謝謝!簡單,幫助了很多 – Meowbits 2012-04-13 21:06:26

4

在您的示例中的assertEquals將方法調用的返回值與預期值,你想要什麼這是不,當然還有不會是如果返回值出現預期的異常。移動的assertEquals到catch塊:

@Test 
public void testTC3() 
{ 
    try { 
     Shipping.shippingCost('P', -5); 
     fail(); // if we got here, no exception was thrown, which is bad 
    } 
    catch (Exception e) { 
     final String expected = "Legal Values: Package Type must be P or R"; 
     assertEquals(expected, e.getMessage()); 
    }   
} 
+0

沒有看到你的答案,我返工閱讀您的答覆後,我的代碼。謝謝! – Meowbits 2012-04-13 21:08:49

0

的Java 8解決方案

這是一個實用的功能,我寫道:

public final <T extends Throwable> T expectException(Class<T> exceptionClass, Runnable runnable) 
{ 
    try 
    { 
     runnable.run(); 
    } 
    catch(Throwable throwable) 
    { 
     if(throwable instanceof AssertionError && throwable.getCause() != null) 
      throwable = throwable.getCause(); //allows "assert x != null : new IllegalArgumentException();" 
     assert exceptionClass.isInstance(throwable) : throwable; //exception of the wrong kind was thrown. 
     assert throwable.getClass() == exceptionClass : throwable; //exception thrown was a subclass, but not the exact class, expected. 
     @SuppressWarnings("unchecked") 
     T result = (T)throwable; 
     return result; 
    } 
    assert false; //expected exception was not thrown. 
    return null; //to keep the compiler happy. 
} 

taken from my blog

如下使用它:

@Test 
public void testThrows() 
{ 
    RuntimeException e = expectException(RuntimeException.class,() -> 
     { 
      throw new RuntimeException("fail!"); 
     }); 
    assert e.getMessage().equals("fail!"); 
} 

另外,如果你想閱讀一些原因,你應該assertTrue你異常的消息是等於一個特定的值時,看到以下內容:https://softwareengineering.stackexchange.com/a/278958/41811

1

作品完美的我。

try{ 
    assertEquals("text", driver.findElement(By.cssSelector("html element")).getText()); 
    }catch(ComparisonFailure e){ 
     System.err.println("assertequals fail"); 
    } 

如果失敗的assertEquals將ComparisonFailure處理它

0

這是很好,使主張在一個乾淨的方式異常。

例子:

// given: an empty list 
List myList = new ArrayList(); 

// when: we try to get the first element of the list 
when(myList).get(1); 

// then: we expect an IndexOutOfBoundsException 
then(caughtException()) 
     .isInstanceOf(IndexOutOfBoundsException.class) 
     .hasMessage("Index: 1, Size: 0") 
     .hasNoCause();