在使用 JMockit 时,我希望能够像这样在构造函数调用时抛出一个异常:
new Expectations(){
{
new FirefoxDriver();//Want to throw IllegalStateException here but how?
}
};
在使用 JMockit 时,我希望能够像这样在构造函数调用时抛出一个异常:
new Expectations(){
{
new FirefoxDriver();//Want to throw IllegalStateException here but how?
}
};
要为记录的期望值指定结果,请将其分配到result
字段中(无论是返回值还是要抛出的异常):
new Expectations() {{
someMockedMethodOrConstructorInvocation(...); result = new IllegalStateException();
}};
我们应该将要模拟的类作为参数添加到测试用例的方法中。通过使用结果,我们可以模拟方法的输出。
@Test
public void testCase(@Mocked final ClassToMock classToMockObject){
new NonStrictExpectations() {
{
classToMockObject.methodToMock();result=NullPointerException();
}};
classToMockObject.methodToMock(); //calling the method to throw exception
}
@Mocked
")声明一个 mock 字段或 mock 参数。否则,实际的方法或构造函数确实会被执行。 - Rogérionew Expectations(FirefoxDriver.class) {{...}};
就可以了,但我对JMockit还不太自信...也许在方法中添加@Test(expected = IllegalStateException)
,然后在期望块之后调用new FirefoxDriver();
? - Amos M. Carpenter