While using JMockit I want to throw an exception upon a constructor invocation like this:
new Expectations(){
{
new FirefoxDriver();//Want to throw IllegalStateException here but how?
}
};
While using JMockit I want to throw an exception upon a constructor invocation like this:
new Expectations(){
{
new FirefoxDriver();//Want to throw IllegalStateException here but how?
}
};
To specify the result for a recorded expectation, assign it (either values to return or exceptions to throw) to the result
field:
new Expectations() {{
someMockedMethodOrConstructorInvocation(...); result = new IllegalStateException();
}};
we should add the class to be mocked as parameters in the method of the test case.and using result, we can mock the outcome of the method.
@Test
public void testCase(@Mocked final ClassToMock classToMockObject){
new NonStrictExpectations() {
{
classToMockObject.methodToMock();result=NullPointerException();
}};
classToMockObject.methodToMock(); //calling the method to throw exception
}