Java 在jmockit中,如何模拟void方法在第一次调用时抛出异常而不在后续调用中抛出异常?
我可以使void方法抛出如下异常:Java 在jmockit中,如何模拟void方法在第一次调用时抛出异常而不在后续调用中抛出异常?,java,jmockit,Java,Jmockit,我可以使void方法抛出如下异常: class TestClass { public void send(int a) {}; } @Mocked private TestClass mock; @Test public void test() throws Exception { new Expectations() { { mock.send(var1); this.result = new Exceptio
class TestClass {
public void send(int a) {};
}
@Mocked
private TestClass mock;
@Test
public void test() throws Exception {
new Expectations() {
{
mock.send(var1);
this.result = new Exception("some exception");
}
};
}
但是,如果我希望void方法在第一次调用时抛出异常,而不是在后续调用时抛出异常,那么这些方法似乎不起作用:
@Test
public void test() throws Exception {
new Expectations() {
{
mock.send(var1);
this.result = new Exception("some exception");
this.result = null;
}
};
}
或
它们都不会引发异常
使用JMockit可以实现这一点吗?从文档和文档中我不清楚。以下测试对我来说很好:
static class TestClass { void send(int a) {} }
@Mocked TestClass mock;
int var1 = 1;
@Test
public void test() {
new Expectations() {{
mock.send(var1);
result = new Exception("some exception");
result = null;
}};
try { mock.send(var1); fail(); } catch (Exception ignore) {}
mock.send(var1);
}
static class TestClass { void send(int a) {} }
@Mocked TestClass mock;
int var1 = 1;
@Test
public void test() {
new Expectations() {{
mock.send(var1);
result = new Exception("some exception");
result = null;
}};
try { mock.send(var1); fail(); } catch (Exception ignore) {}
mock.send(var1);
}