【发布时间】:2023-03-09 18:43:01
【问题描述】:
我们正在使用 Testng 6.8.8 + Mockito 1.10.19,显然我们不能使用MockitoJUnitRunner,但验证框架仍然有效!
在这个thread 我读到它不应该是这样的。有人可以解释吗?这是因为我们还有@Before* 回调和MockitoAnnotations.initMocks(this)吗?
我的代码:
public class MealTransformerTest {
MealTransformer mealTransformer = new MealTransformer();
@Test(expectedExceptions = NotImplementedException.class)
public void shouldThrowException() throws NotImplementedException {
mealTransformer.transform(any(),
any(),
any(),
any());
}
} 在这个特定的测试中没有任何失败,但是当我运行该套件时,Mockito 会告诉我匹配器的错误使用。
我也可以这样做:
public class MealTransformerTest {
MealTransformer mealTransformer = new MealTransformer();
//I don't need it in the tests. But I need at least 1 @Mock or @Spy to trigger framework validation
@Mock
private CloneUtils cloneUtils;
@BeforeMethod
void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test(expectedExceptions = NotImplementedException.class)
public void shouldThrowException() throws NotImplementedException {
mealTransformer.transform(any(),
any(),
any(),
any());
}
@Test(expectedExceptions = NotImplementedException.class)
public void shouldThrowException123() throws NotImplementedException {
mealTransformer.transform(any(),
any(),
any(),
any());
}
}
我收到:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:
....
不要误会我的意思,我真的很喜欢它的工作原理,但是看到没有@RunWith(MockitoJUnitRunner.class) 时我很惊讶。
【问题讨论】:
-
您可以在参数匹配器中放置一个断点,看看为什么即使没有 MockitoJUnitRunner 仍然会生成此异常。即使没有运行器,如果使用不正确,Mockito 类仍然会抛出异常。跑步者只是在此之上提供了一些“集成级”验证。
-
MockitoJUnitRunner用于 JUnit。 TestNG 将忽略它。因此,拥有与否不会随着 testng 测试而改变。 -
@JulienHerr,当然。我对此毫无疑问,我已经使用 testNG 和 JUnit 有一段时间了。
-
@nullpointer 提供指向我在问题中提到的同一线程的链接并不是很有帮助,但谢谢。
标签: java unit-testing junit mockito testng