使用 JUnit 和 Mockito 测试方法结果的最佳方式
Best way to test a method's outcome with JUnit and Mockito
我正在尝试测试方法:
@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() requires an argument which has to be 'a method call on a mock'.
For example:
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()。
任何帮助将不胜感激!
我会遵循 XP 规则,做可能可行的最简单的事情。你不需要模拟任何东西,所以直接使用对象。
private AppleGreenColorPredicate classUnderTest;
@Before
public void setUp() {
classUnderTest = new AppleGreenColorPredicate();
}
@Test
public void nullTest() {
assertFalse(classUnderTest.test(null));
}
@Test
public void green() {
Apple a = new Apple();
a.setColor("green");
assertTrue(classUnderTest.test(a));
}
@Test
public void notGreen() {
Apple a = new Apple();
a.setColor("red");
assertFalse(classUnderTest.test(a));
}
在您的 mockito 代码中,您似乎在模拟您正在测试的内容,也就是说,您不会在代码中发现任何问题,因为您的测试仅调用模拟。这给人一种错误的安全感。只在必要时才嘲笑。模拟被测 class 的依赖关系,而不是被测的实际 class。
我正在尝试测试方法:
@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() requires an argument which has to be 'a method call on a mock'. For example: 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()。
任何帮助将不胜感激!
我会遵循 XP 规则,做可能可行的最简单的事情。你不需要模拟任何东西,所以直接使用对象。
private AppleGreenColorPredicate classUnderTest;
@Before
public void setUp() {
classUnderTest = new AppleGreenColorPredicate();
}
@Test
public void nullTest() {
assertFalse(classUnderTest.test(null));
}
@Test
public void green() {
Apple a = new Apple();
a.setColor("green");
assertTrue(classUnderTest.test(a));
}
@Test
public void notGreen() {
Apple a = new Apple();
a.setColor("red");
assertFalse(classUnderTest.test(a));
}
在您的 mockito 代码中,您似乎在模拟您正在测试的内容,也就是说,您不会在代码中发现任何问题,因为您的测试仅调用模拟。这给人一种错误的安全感。只在必要时才嘲笑。模拟被测 class 的依赖关系,而不是被测的实际 class。