john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

junit easymock

/*
    //EASYMOCK cannot override final classes
    //java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.System

    System mockLinux = EasyMock.createMock(Properties.class);
    EasyMock.expect( mockLinux.getProperty( "os.name" )).andReturn( "Linux" );
    EasyMock.expect( mockLinux.getProperty( "file.separator" )).andReturn( "/" );
    EasyMock.expect( mockLinux.getProperty( "line.separator" )).andReturn( "\n" );
    EasyMock.expect( mockLinux.getProperty( "java.io.tmpdir" )).andReturn( "/tmp" );
    EasyMock.replay( mockLinux );


    @Test
    public void testGetTempDirectoryLinux() {
        FileSystem currentSystem = new FileSystem( mockLinux );
        assertTrue( currentSystem.isLinux() );
        assertEquals( "/tmp" , currentSystem.getTempDirectory() );
        EasyMock.verify(mockLinux);
    }

*/


//INCOMPLETE as it does not test anything but good example of handling VOID calls
    @Test
    public void test_setEnforcePinLockOnAccount() throws Exception {
        // Given:
        String platform = ClientConstants.PLATFORM_MOBILE_OID;

        mockSessionManagerServiceImpl.setEnforcePinLockOnAccount( request, accountId, platform, true );
        EasyMock.expectLastCall();  // once per VOID return value
        EasyMock.expect( mockSessionManagerServiceImpl.isEnabledEnforcePinLockOnAccount( request, platform, accountId ) ).andReturn( true );
        EasyMock.replay( mockSessionManagerServiceImpl );

        // When:
        mockSessionManagerServiceImpl.setEnforcePinLockOnAccount( request, accountId, platform, true );

        // Then:
        assertTrue( mockSessionManagerServiceImpl.isEnabledEnforcePinLockOnAccount( request, platform, accountId ));
        EasyMock.verify( mockSessionManagerServiceImpl );
    }




Download the .zip and extract the easymock-3.1.jar
(http://easymock.org/Downloads.html)

Add the EasyMock jar file (easymock.jar) to your classpath
( Right click on Project Name -> Java Build Path -> Libraries (tab) -> Add External JARs (button) -> Browse )



import org.easymock.EasyMock;       //mock objects must have desired results explicitly coded

public class



        HttpServletRequest testRequest = EasyMock.createStrictMock( HttpServletRequest.class );     //strictmock forces correct method order calling
        HttpSession testSession = EasyMock.createStrictMock( HttpSession.class );
        String sessionId = EasyMock.createStrictMock( String.class );

        EasyMock.expect( testRequest.getSession( false ) ).andReturn( null );           //record expected behaviour, e.g. getsession(false) returns null
        EasyMock.expect( testRequest.getSession() ).andReturn( testSession );                   //record expected behaviour

//      EasyMock.expect( testSession.setMaxInactiveInterval( 60 ).andReturn( session ) );
//      EasyMock.expect( testSession.getId().andReturn( "1" ) );
//  session.setMaxInactiveInterval( minutes * 60 );

later on you can replay

        EasyMock.replay( testRequest );
//      EasyMock.replay( session );

  • « junit testing maven class resources skip tests
  • log4j dynamic configuration »

Published

Jan 4, 2013

Category

java

~214 words

Tags

  • easymock 1
  • java 252
  • junit 8