我正在尝试测试该方法:
@Override
public boolean test(Apple apple) {
if (apple == null) {
return false;
}
return "green".equals(apple.getColor());
}
我的第一个猜测是通过以下方式对其进行测试:
package io.warthog.designpatterns.behaviorparameterization.impl;
import io.warthog.designpatterns.behaviorparameterization.Apple; import org.junit.Test;
import static org.mockito.Mockito.when;
public class AppleGreenColorPredicateTest {
private AppleGreenColorPredicate classUnderTest = new AppleGreenColorPredicate();
@Test
public void test_WithGreenApple_ReturnsTrue() {
Apple apple = new Apple();
apple.setColor("green");
when(classUnderTest.test(apple)).thenReturn(true);
}
}
但它给了我错误信息:
org.mockito.exceptions.misusing.MissingMethodInvocationException: when() 需要一个参数,该参数必须是“模拟的方法调用”。例如:when(mock.getArticles()).thenReturn(articles);
所以我最终做了:
package io.warthog.designpatterns.behaviorparameterization.impl;
import io.warthog.designpatterns.behaviorparameterization.Apple; import org.junit.Assert; import org.junit.Test;
public class AppleGreenColorPredicateTest {
private AppleGreenColorPredicate classUnderTest = new AppleGreenColorPredicate();
@Test
public void test_WithGreenApple_ReturnsTrue() {
Apple apple = new Apple();
apple.setColor("green");
Assert.assertEquals(true, classUnderTest.test(apple));
}
}
这里的问题是,您何时建议使用 Mockinto.when() 方法以及何时使用 Assert.equals()。
任何帮助将不胜感激!