2016-11-10 91 views
0

我有一個名爲計算器加,減,除法和乘法如何正確使用JUnit assertThat?

public class Calculator{ 

     public int add(int a, int b) { 
      return a + b; 
     } 

     public int subtract(int a, int b) { 
      return a - b; 
     } 

     public double multiply(double a, double b) { 
      return a * b; 
     } 

     public double divide(double a, double b) { 
      if (b == 0) { 
       throw new ArithmeticException("Division by zero."); 
      } 
      return a/b; 
     } 

    } 

我使用Maven項目,我的pom.xml文件的四種基本操作類:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>br.usp.icmc</groupId> 
    <artifactId>Calculadora</artifactId> 
    <version>0.0.1</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.hamcrest</groupId> 
      <artifactId>hamcrest-library</artifactId> 
      <version>1.3</version> 
     </dependency> 
    </dependencies> 
</project> 

我JUnit中創建了一個測試如下:

public void testSumWithAssertThat() { 
     int expectedValue = 2; 
     int returnedValue = calculator.add(1, 1);  
     assertThat(returnedValue, is(expectedValue)); 
    } 

,我發現了以下異常:

java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package 

你爲什麼拋出異常?這個簡單的代碼有什麼問題?

+1

這裏閱讀http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertThat(java.lang.String,T org.hamcrest.Matcher) – DimaSan

回答

1

確保hamcrest.jar是包含在類路徑將解決問題的的JUnit庫之前。