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

1673Re: [easymock] Re: Intercepting any method invocation on a mock

Expand Messages
  • Eduardo Simioni
    Aug 14, 2013
      The cglib solution works!


      On Tue, Aug 13, 2013 at 2:53 PM, Henri Tremblay <henri.tremblay@...> wrote:

      It's true that in your case, you don't really need EasyMock.

      Using the cglib Enhancer is enough.

      Object mock = Enhancer.create(InputStream.class, new MethodInterceptor() {
          public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args, MethodProxy proxy) throws Throwable {
              throw new IOException(method.getName() + " called!");

      On 13 August 2013 11:27, Eduardo Simioni <simioni.eduardo@...> wrote:


      This option didn't work as is with EasyMock 3.2.
      ClassProxyFactory is not generic and the createProxy method receives more parameters.
      I'll try to figure it out when I have some time.



      On Mon, Aug 12, 2013 at 7:14 PM, joshuaball65495 <joshuaball65495@...> wrote:

      Here's one way:

      InputStream inputStream = new ClassProxyFactory<InputStream>().createProxy(InputStream.class, new InvocationHandler() {
      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