I am trying to test the method:
@Override
public boolean test(Apple apple) {
if (apple == null) {
return false;
}
return "green".equals(apple.getColor());
}
My first guess was to test it in the following way:
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);
}
}
But it gave me the error message:
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);
So I ended up doing:
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));
}
}
The question here is, when would you recommend using the Mockinto.when() approach and when the Assert.equals().
Any help will be greatly appreciated!