Question

I am trying to write up a few tests using JMock and the following code will be added:

db = context.mock(DBResultQueryExecutor.class);
context.checking(new Expectations() {{
     oneOf (db).prepareQuery(query);
     will(throwException(new QueryException("Test")));
}});

Now, my DBResultQueryExecutor is defined as follows:

public interface DBResultQueryExecutor{
...
void executeQuery() throws QueryException; //This is a checked exception
... }

In JMock I am simply trying to specify that the method will be called at some point in the future exactly once. That is fine except for the fact that Eclipse is complaining about the unhandled exception WITHIN the expectation setup. I know it will be thrown later on and I have no business checkingit within the expectation.

Am I doing something terribly wrong here or is there a simple remedy for this situation? Thanks.

Was it helpful?

Solution

I just got some great help from my colleagues for this issue; the solution was to add "throws Exception" to your test method descriptor. The explanation is still not crystal clear to me, but it's something along the lines of 'Java doesn't know that you're not actually calling the method' in the Expectations {} block. Seems to me, Java should know - but it doesn't.

I hope that helps - I was also convinced that Eclipse was just wrong.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top