Junit 我们什么时候应该使用嘲弄与嘲弄?
如果使用JMock编写带有模拟的Java单元测试,我们应该使用Junit 我们什么时候应该使用嘲弄与嘲弄?,junit,mocking,jmock,Junit,Mocking,Jmock,如果使用JMock编写带有模拟的Java单元测试,我们应该使用 Mockery context = new Mockery() 或 这两者之间的区别是什么,什么时候应该使用哪一种?在JUnit4中使用JMock时,您可以利用JMock测试运行程序来避免一些样板代码。执行此操作时,必须使用JUnit4模拟,而不是常规模拟 下面是如何构造JUnit 4测试: @RunWith(JMock.class) public void SomeTest() { Mockery context = new
Mockery context = new Mockery()
或
这两者之间的区别是什么,什么时候应该使用哪一种?在JUnit4中使用JMock时,您可以利用JMock测试运行程序来避免一些样板代码。执行此操作时,必须使用JUnit4模拟,而不是常规模拟 下面是如何构造JUnit 4测试:
@RunWith(JMock.class)
public void SomeTest() {
Mockery context = new JUnit4Mockery();
}
主要优点是无需在每次测试中调用
assertessatified
,它在每次测试后自动调用。@Rhys不是junit4mockry
取代了调用assertessatified
,而是JMock.class
(与@RunWith
结合使用)。当您创建一个常规的模拟时,您不需要调用assertessatified
junit4mockry
转换错误
默认情况下,预期异常在Junit中报告为ExpectationError
,例如,使用
Mockery context = new Mockery();
你会得到
unexpected invocation: bar.bar()
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
使用,
Mockery context = new JUnit4Mockery();
你会得到
java.lang.AssertionError: unexpected invocation: bar.bar()
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
JUnit4Mockry将ExpectationError转换为JUnit处理的java.lang.AssertionError。最终结果是,它将在您的JUnit报告中显示为一个失败(使用JUnit4Mockry),而不是一个错误,更好的是,根据使用@Rule和避免@run,您可能需要使用它来实现其他系统:
public class ATestWithSatisfiedExpectations {
@Rule
public final JUnitRuleMockery context = new JUnitRuleMockery();
private final Runnable runnable = context.mock(Runnable.class);
@Test
public void doesSatisfyExpectations() {
context.checking(new Expectations() {
{
oneOf(runnable).run();
}
});
runnable.run();
}
}
我想那也是我的困惑。
public class ATestWithSatisfiedExpectations {
@Rule
public final JUnitRuleMockery context = new JUnitRuleMockery();
private final Runnable runnable = context.mock(Runnable.class);
@Test
public void doesSatisfyExpectations() {
context.checking(new Expectations() {
{
oneOf(runnable).run();
}
});
runnable.run();
}
}