【问题标题】:How to use JUnit assertThat correctly? [duplicate]如何正确使用 JUnit assertThat? [复制]
【发布时间】:2016-11-10 17:44:04
【问题描述】:

我有一个叫Calculator的类,有加减乘除四种基本运算

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

为什么要抛出异常?这段简单的代码出了什么问题?

【问题讨论】:

标签: java junit hamcrest


【解决方案1】:

确保 hamcrest.jar 位于类路径中包含的 JUnit 库 将解决问题之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2016-03-09
    • 2014-03-31
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    相关资源
    最近更新 更多