Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] JMock doesn't seem to be invoked when mocking a concrete class

Viggo Navarsete

2009-11-23

Replies: Find Java Web Hosting

Author LoginPost Reply
I'm having a unit test which is supposed to test a Service. The Service in turn calls another class. I want to mock the invocations to the class the Service uses.

MyTest-->Service-->AConcreteClass

So, MyTest executes a method on Service, which in turn execute a method on a concrete class, AConcreteClass.
I want to mock the execution on the AConcreteClass in Service, but it doesn't seem to mock it.

Some code:

************************************************************************************************************************************************************************************************
MyTest:

@Test
public void testDataLoad() throws Exception {

        Mockery context = new Mockery() {{
            setImposteriser( ClassImposteriser.INSTANCE );
        }};

        final VirtualInstanceContextHolder vich = context.mock( VirtualInstanceContextHolder.class );
        final VirtualInstanceContext vic = context.mock( VirtualInstanceContext.class );
        final ApplicationContext ac = context.mock( ApplicationContext.class );
        final Persistence persistence = context.mock( Persistence.class );
        final Map<String, String> emMap = new HashMap<String, String>();

        context.checking( new Expectations() {{
            allowing( vich );
                will( returnValue( new VirtualInstanceContext()));
            allowing( vic ).getApplicationContext();
                will( returnValue( new AutowiringXmlWebApplicationContext() ));
            allowing( ac ).getParent();
            allowing( ac ).getBean( "SecureDTOFactory" );
                will( returnValue( new DTOFactory() ) );
            allowing( persistence).createEntityManagerFactory( "epcisTIXPU", emMap );
                will( returnValue( Persistence.createEntityManagerFactory( "epcisTIXPU" ).createEntityManager()));
        }});


       results = service.simpleEventQuery(new QueryParams()); // execute the actual method on Service


************************************************************************************************************************************************************************************************
Service:

   public List<EventDetailsDTO> simpleEventQuery( QueryParams queryParam) {
 
       DTOFactory dtoFactory = getDTOFactory(); // I want to mock the method invocations in the private getDTOFactory!!
   }

    private DTOFactory getDTOFactory() {
        VirtualInstanceContext vic = VirtualInstanceContextHolder.getContext(); // I get back an object here
        ApplicationContext ac = vic.getApplicationContext(); // This one returns null and is what I want to figure out why!!
        ApplicationContext acParent = ac.getParent();
        DTOFactory dtoFactory = (DTOFactory) acParent.getBean( "SecureDTOFactory" );
        return dtoFactory;
    }







My environment:
- Maven 2.0.9
- JUnit 4.4
- JDK 1.6.0_16

©2008 gg3721.com - Jax Systems, LLC, U.S.A.