hamcrest 匹配器不匹配相等的问题

Problem with hamcrest matcher not matching equal

我遇到了以下代码的问题,我有点困惑

public class EchoApp {
    public static void main(String[] args) {
        for (String string : args) {
            System.out.println(string);
        }
    }
}

以下测试失败:

import static org.hamcrest.CoreMatchers.equalTo;
import static org.mockito.Mockito.*;
import java.io.PrintStream;
import org.junit.Test;

public class AppTest {
    @Test
    public void test() {
        Input input = new Input("Hello!", "World!");
        Output output = new Output("Hello!", "World!");
        PrintStream out = mock(PrintStream.class);
        System.setOut(out);
        EchoApp.main(input.wrap());
        output.value.forEach(i -> {
            verify(out).println(equalTo(i));
        });
    }
}



import java.util.Arrays;
import java.util.List;

class Input extends Immutable<List<String>> {
    Input(final List<String> values) {
        super(values);
    }

    Input(String... values) {
        super(Arrays.asList(values));
    }

    String[] wrap() {
        return value.toArray(new String[value.size()]);
    }

    @Override
    public String toString() {
        return "Input [value=" + value + "]";
    }

}

import java.util.Arrays;
import java.util.List;

class Output extends Immutable<List<String>> {
    Output(List<String> values) {
        super(values);
    }

    Output(String... values) {
        super(Arrays.asList(values));
    }

    @Override
    public String toString() {
        return "Output [value=" + value + "]";
    }
}


class Immutable<T> {
    final T value;

    Immutable(T value) {
        this.value = value;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Immutable other = (Immutable) obj;
        if (value == null) {
            if (other.value != null)
                return false;
        } else if (!value.equals(other.value))
            return false;
        return true;
    }

}

参数不同!通缉: printStream.println("Hello!"); -> 在 AppTest.lambda$0(AppTest.java:24) 实际调用有不同的参数: printStream.println("Hello!"); -> 在 EchoApp.main(EchoApp.java:7)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at AppTest.lambda[=12=](AppTest.java:24)
at java.util.Arrays$ArrayList.forEach(Arrays.java:3880)
at AppTest.test(AppTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

设置断点到 Immutable#equals 永远不会命中。 如果有任何线索可以让测试达到预期目的,我将不胜感激 ;)

我认为如果你使用 Mockito 匹配器(不是 hamcrest 匹配器)它会起作用:org.mockito.Matchers#eq(T)。 当涉及到这些匹配器时,自动完成很差,因为它们只有 return 个值。 org.mockito.Matchers 用于验证参数(参见 JavaDoc)。