我在使用以下代码时遇到问题,感到有点困惑
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$0(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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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。 我很高兴有任何线索可以对它打算做的事情进行测试;)
答案 0 :(得分:3)
我认为,如果您使用Mockito Matcher(而不是hamcrest一个),它会起作用:org.mockito.Matchers#eq(T)。 这些匹配器的自动完成功能很差,因为它们仅返回一个值。 org.mockito.Matchers用于验证参数(请参阅JavaDoc)。