tags:

views:

279

answers:

2

I want to mock a method in abstract class say 'A' and also need to pass and instance of type A to the methods I am unit testing.

Is there a way to create an instance using Jmockit like Mockit.newemptyProxy How do I solve this scenario

A: 

Maybe this is a silly question, but do you actually need JMockit in this situation? Can't you just make a subclass of A and override the method you want to mock? Something like this:

class MyMockA extends A {

    @Override
    int myMethod(int x) {
        // do stuff
    }
}

@Test
public void test_A_handler() {
    A a = new MyMockA();
    A_handler testSubject = new A_handler();
    assertEquals(123, testSubject.handleA(a));
}
Chris B
A: 

You could do it simply like this:


@Test
public void mockAbstractClassA(final A mock)
{
   new Expectations() {{
      mock.doThis();
      mock.doThat(); returns(123);
   }};

   new ClassUnderTest(mock).doStuff();
}

Note the "A mock" parameter in the test method.

Rogerio