Проблема с совпадением подколенного сухожилия не совпадает - PullRequest
0 голосов
/ 01 сентября 2018

У меня проблемы со следующим кодом, и я немного озадачен

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 # равно никогда не срабатывает. Буду признателен за любую подсказку, чтобы получить тест на то, что он должен делать;)

1 Ответ

0 голосов
/ 01 сентября 2018

Я думаю, что это работает, если вы используете Mockito Matcher (не Hamcrest): org.mockito.Matchers # eq (T). Автозаполнение плохое, когда дело доходит до этих Matchers, поскольку они только возвращают значение. org.mockito.Matchers предназначены для проверки аргументов (см. JavaDoc).

...