Mockito 长不为零 ArgumentMatcher
Mockito Long not Zero ArgumentMatcher
我正在尝试使用 Mockito (mockito-core)1.10.19 将非常旧的 Java 遗留测试重构为 Mockito 4。
我在遗产中有以下匹配器
private static long nonZeroLong() {
return longThat(not(0L));
}
应该为以下模拟提供 arg 匹配器
doAnswer(invocation -> {
Runnable originalTask = invocation.getArgumentAt(0, Runnable.class);
Runnable runnable = () -> {
/*
LOCK is need here to emulate waiting on ConcurrentHashMap.compute method.
It is uses `synchronized` block internally, so any technique which can throw
interrupted exception for waiting can't be used here
*/
synchronized (LOCK) {
}
originalTask.run();
scheduledFutureExecuted.set(true);
futureExecutedSignal.countDown();
};
invocation.getArguments()[0] = runnable;
ScheduledFuture res = (ScheduledFuture) invocation.callRealMethod();
scheduledFutureRef.set(res);
return res;
}).when(pool).schedule(any(Runnable.class), nonZeroLong(), any());
我不是 mockito-guro,但首先我完全不确定这种原始方式是否适合此调用,但它是原始方式。
由于在 Mockito 4 中这个参数匹配器不再存在,我将其重构为:
private static long nonZeroLong() {
return longThat(argument -> argument != 0);
}
但是我仍然不确定我提供的内容是否与遗留版本中的原始匹配器相同。
我的问题是 - 我的匹配器会提供与原始匹配器相同的逻辑吗?
行为相同。
但在您的情况下,您创建了自定义 ArgumentMatcher
。
最好使用 mockito 附加匹配器 not(eq(value))
private static long nonZeroLong() {
return not(eq(0L));
}
至少它们提高了错误输出的可读性,并且它们对任何类型都有灵活的实现。
Mokito 匹配器输出:
Argument(s) are different! Wanted:
bean.print(not(0L)); // We see actual problem
-> at TestObject.testZeroMathers(TestObject.java:28)
Actual invocations have different arguments:
bean.print(0L);
-> at TestObject.testZeroMathers(TestObject.java:26)
自定义匹配器输出:
Argument(s) are different! Wanted:
bean.print(<custom argument matcher>); // We do not see actual problem
-> at TestObject.testZeroLongThat(TestObject.java:35)
Actual invocations have different arguments:
bean.print(0L);
-> at TestObject.testZeroLongThat(TestObject.java:33)
测试示例:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.AdditionalMatchers.not;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class TestObject {
@Spy
Bean bean = new Bean();
@Test
public void test() {
bean.print(1L);
verify(bean, times(1)).print(nonZeroLong());
verify(bean, times(1)).print(nonZeroLongThat());
}
@Test
public void testZeroMathers() {
bean.print(0L);
verify(bean, times(1)).print(nonZeroLong());
}
@Test
public void testZeroLongThat() {
bean.print(0L);
verify(bean, times(1)).print(nonZeroLongThat());
}
private static long nonZeroLongThat() {
return longThat(argument -> argument != 0);
}
private static long nonZeroLong() {
return not(eq(0L));
}
}
public class Bean {
public void print(Long l) {
System.out.println("Hi, I'm Bean " + l);
}
}
我正在尝试使用 Mockito (mockito-core)1.10.19 将非常旧的 Java 遗留测试重构为 Mockito 4。
我在遗产中有以下匹配器
private static long nonZeroLong() {
return longThat(not(0L));
}
应该为以下模拟提供 arg 匹配器
doAnswer(invocation -> {
Runnable originalTask = invocation.getArgumentAt(0, Runnable.class);
Runnable runnable = () -> {
/*
LOCK is need here to emulate waiting on ConcurrentHashMap.compute method.
It is uses `synchronized` block internally, so any technique which can throw
interrupted exception for waiting can't be used here
*/
synchronized (LOCK) {
}
originalTask.run();
scheduledFutureExecuted.set(true);
futureExecutedSignal.countDown();
};
invocation.getArguments()[0] = runnable;
ScheduledFuture res = (ScheduledFuture) invocation.callRealMethod();
scheduledFutureRef.set(res);
return res;
}).when(pool).schedule(any(Runnable.class), nonZeroLong(), any());
我不是 mockito-guro,但首先我完全不确定这种原始方式是否适合此调用,但它是原始方式。
由于在 Mockito 4 中这个参数匹配器不再存在,我将其重构为:
private static long nonZeroLong() {
return longThat(argument -> argument != 0);
}
但是我仍然不确定我提供的内容是否与遗留版本中的原始匹配器相同。
我的问题是 - 我的匹配器会提供与原始匹配器相同的逻辑吗?
行为相同。
但在您的情况下,您创建了自定义 ArgumentMatcher
。
最好使用 mockito 附加匹配器 not(eq(value))
private static long nonZeroLong() {
return not(eq(0L));
}
至少它们提高了错误输出的可读性,并且它们对任何类型都有灵活的实现。
Mokito 匹配器输出:
Argument(s) are different! Wanted:
bean.print(not(0L)); // We see actual problem
-> at TestObject.testZeroMathers(TestObject.java:28)
Actual invocations have different arguments:
bean.print(0L);
-> at TestObject.testZeroMathers(TestObject.java:26)
自定义匹配器输出:
Argument(s) are different! Wanted:
bean.print(<custom argument matcher>); // We do not see actual problem
-> at TestObject.testZeroLongThat(TestObject.java:35)
Actual invocations have different arguments:
bean.print(0L);
-> at TestObject.testZeroLongThat(TestObject.java:33)
测试示例:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.AdditionalMatchers.not;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class TestObject {
@Spy
Bean bean = new Bean();
@Test
public void test() {
bean.print(1L);
verify(bean, times(1)).print(nonZeroLong());
verify(bean, times(1)).print(nonZeroLongThat());
}
@Test
public void testZeroMathers() {
bean.print(0L);
verify(bean, times(1)).print(nonZeroLong());
}
@Test
public void testZeroLongThat() {
bean.print(0L);
verify(bean, times(1)).print(nonZeroLongThat());
}
private static long nonZeroLongThat() {
return longThat(argument -> argument != 0);
}
private static long nonZeroLong() {
return not(eq(0L));
}
}
public class Bean {
public void print(Long l) {
System.out.println("Hi, I'm Bean " + l);
}
}