JMockit Expectation API : How to throw an exception upon method/constructor invocation

Affan Hasan picture Affan Hasan · Mar 11, 2015 · Viewed 21.6k times · Source

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?
        }
};

Answer

Rogério picture Rogério · Mar 11, 2015

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();
}};