« Return to Thread: Mocking a Concrete class with JMock

Mocking a Concrete class with JMock

by T P D-2 :: Rate this Message:

Reply to Author | View in Thread



I have class with a forwarding method foo:

void foo( Concrete c, String s ) { c.bar( s ); }

I wish to test that foo in fact forwards. Unfortunately for me, Concrete
is a class in a third-party library, and is a Concrete type, not an
interface. Thus I must use ClassImposterizer in Jmock to mock Concrete,
so in my testcase I do this:

@Test
public final void testFoo() {
    Mockery context = new JUnit4Mockery() {{
       setImposteriser(ClassImposteriser.INSTANCE);
    }};

   final Concrete c = context.mock(Concrete.class);
   final String s = "xxx" ;

   // expectations
   context.checking(new Expectations() {{

      oneOf (c).bar(s); // exception gets thrown from here
   }});


   new ClassUnderTest.foo( c, s );
   context.assertIsSatisfied();

}

Unfortunately, Concrete.bar in turn calls a method that throws. That
method is final, so I can't override it. Further, even if I comment out
the line new ClassUnderTest.foo( c, s );, the exception is thrown when
JMock sets up exceptions, not when foo is called.

So how can I test that method ClassUnderTest.foo does forward to
Concrete.bar?

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: Mocking a Concrete class with JMock