使用带有@Capturing
注释的JMockit实现单元测试
以及"捕获"的验证块变量我有2个结果:
IntelliJ:我可以成功运行和调试,验证验证行为是否正确。
使用mvn test
使用命令行(cmd)执行相同的测试代码会导致'缺少调用'。
如果使用cmd,@Capturing
似乎表现得像@Mocked
。因为如果我将@Capturing
更改为@Mocked
,确切的行为。
这是什么原因?
设置:
IntelliJ:2017.3.4
Java 8
JMockit 1.35
答案 0 :(得分:0)
参考:
@Test
public void test(final @Mocked ProfileMgr profileMgr,
final @Capturing Manager manager) throws Exception {
final String id = “1234”;
Final String modelId = “567”;
final Map<String, String> effectiveProps = ImmutableMap.of("test.key", "test.value",
"ppp.key", "ppp.value"
);
final Optional<String> option = Optional.of("123");
new Expectations() {{
profileMgr.mergedProperties(
id,IMPL_1,
(Map<String, String>) any,
option
);
result = effectiveProps;
}};
this.mainManager
.run(
id,
modelId,
new JobRequestWrapper<>(config, IMPL_1, option);
);
new Verifications() {{
final Map<String, String> params;
manager.execute(
anyString,
anyString,
(CalculationConfig) any,
params = withCapture()
);
times = 1;
assertThat(params)
.withFailMessage("params must have content.")
.isNotNull()
.isNotEmpty();
assertThat(params)
.withFailMessage("params must have same injected entry.")
.isEqualTo(effectiveProps);
}};
}
答案 1 :(得分:-1)
通常,这类问题通常是由于maven在jvm的单次执行中运行多个测试。当您在IntelliJ中运行单个单元测试时,测试很好地隔离,但是当使用maven运行时,一次测试中的全局变量的任何更改都可能对其他测试的执行产生意外影响。
这就是为什么人们应该避免在单元测试中改变(甚至使用)静态变量,如果你有弹簧启用的测试可以改变你的上下文,那就使用@DirtiesContext
等。
我没有足够的具体信息来确定您案例中的确切问题,但我建议您开始寻找可能会改变其他测试行为的内容。使用IntelliJ也可以同时运行多个测试类,这可能更接近于maven如何运行测试。