Loading ...
Sorry, an error occurred while loading the content.

1670Re: Intercepting any method invocation on a mock

Expand Messages
  • joshuaball65495
    Aug 12, 2013
    • 0 Attachment
      Here's one way:

      InputStream inputStream = new ClassProxyFactory<InputStream>().createProxy(InputStream.class, new InvocationHandler() {
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      throw new IOException("Broken stream!");
      }
      });


      --- In easymock@yahoogroups.com, "nofxcwb" <simioni.eduardo@...> wrote:
      >
      > Hi,
      >
      > Is it possible to intercept any method invocation on a mock?
      >
      > With Mockito I can do this:
      >
      > protected InputStream createBrokenInputStream() {
      > InputStream in = mock(InputStream.class, new Answer<Object>() {
      >
      > @Override
      > public Object answer(InvocationOnMock invocation) throws IOException {
      > throw new IOException("Broken stream!");
      > }
      > });
      > return in;
      > }
      >
      > This is very useful to test exception scenarios. I don't know exactly which method will be called on the InputStream and I don't actually care, I just want it to throw an exception when it is ever used.
      >
      > I couldn't find a way to do this with EasyMock. Any ideas?
      >
      > Thanks!
      >
      > Eduardo.
      >
    • Show all 5 messages in this topic