keeganwitt / gmock

Automatically exported from code.google.com/p/gmock
6 stars 2 forks source link

Exceptions should not be wrapped by InvokerInvocationException #46

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The following test should pass:

    void testExceptionsShouldNotBeWrappedByInvokerInvocationException() {
        def loader = mock(JavaLoader)
        loader.load("key").raises(NotFoundException)
        def cache = new JavaCache(loader)
        play {
            assertNull cache.load("key")
        }
    }

JavaCache.load:

    public String load(String key, ILoader loader) {
        if (cache.containsKey(key)) {
            return cache.get(key);
        } else {
            String value;
            try {
                value = loader.load(key);
            } catch (NotFoundException e) {
                value = null;
            }
            cache.put(key, value);
            return value;
        }
    }

ILoader.load:

    String load(String key) throws NotFoundException;

NotFoundException:

package org.gmock.utils;

public class NotFoundException extends Exception {

    public NotFoundException() {
        super();
    }

}

Original issue reported on code.google.com by JohnnyJianHY on 13 Jan 2009 at 4:52

GoogleCodeExporter commented 9 years ago
I think I should give some explanations here:

try {
    value = loader.load(key);
} catch (NotFoundException e) { // the exception is wrapped by
InvokerInvocationException, so it cannot be caught here
    value = null;
}

Original comment by JohnnyJianHY on 16 Jan 2009 at 12:06

GoogleCodeExporter commented 9 years ago
I am having trouble to reproduce the problem here. For some reason the test 
fail for
me on the line:

loader.load("key").raises(NotFoundException)

Which doesn't really make sense for me. Could you send me a patch for the 
problem?

Why do you think the exception would be wrap by a InvokerInvocationException? 
And do
you think we should delay the release until we fix this issue?

Original comment by julien.g...@gmail.com on 17 Jan 2009 at 6:26

GoogleCodeExporter commented 9 years ago
I know what the problem is. Try this:

try {
    value = loader.load(key);
} catch (Exception e) {
    System.out.println(e.getClass()); // you will find it is an 
InvokerInvocationException
}

Seems that is a bug of groovy which gives a bad message.

Original comment by JohnnyJianHY on 18 Jan 2009 at 8:19

GoogleCodeExporter commented 9 years ago
I have fixed the bug and uploaded to the repository. Please have a look.

Original comment by JohnnyJianHY on 18 Jan 2009 at 3:58

GoogleCodeExporter commented 9 years ago
Well done! I had a quick look at your changes and you seems to have modify the 
way
groovy interception work. Was that the issue?

Original comment by julien.g...@gmail.com on 19 Jan 2009 at 8:10

GoogleCodeExporter commented 9 years ago
You are right. You can see that I have rewritten the interceptors in Java, but 
I just
found it is a bug of groovy while casting a closure to an interface, and the 
wrapping
and unwrapping of exceptions isn't handled correctly.

Original comment by JohnnyJianHY on 19 Jan 2009 at 9:25