【发布时间】:2018-01-19 08:27:27
【问题描述】:
我正在使用一些引发异常的库,并且想测试我的代码在引发异常时是否正确运行。存根重载方法之一似乎不起作用。我收到此错误:Stubber 无法应用于 void。不存在变量类型 T 的实例类型,因此 void 确认 T
`public class AnotherThing {
private final Something something;
public AnotherThing(Something something) {
this.something = something;
}
public void doSomething (String s) {
something.send(s);
}
}
public class Something {
void send(String s) throws IOException{
}
void send(int i) throws IOException{
}
}
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class OverloadedTest {
@Test(expected = IllegalStateException.class)
public void testDoSomething() throws Exception {
final Something mock = mock(Something.class);
final AnotherThing anotherThing = new AnotherThing(mock);
doThrow(new IllegalStateException("failed")).when(anotherThing.doSomething(anyString()));
}
}`
【问题讨论】:
标签: java unit-testing mockito