org.jmock.Mock Java Examples

The following examples show how to use org.jmock.Mock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JrpipListenerTest.java    From jrpip with Apache License 2.0 6 votes vote down vote up
public void testExceptionalEvents() throws MalformedURLException,
        InterruptedException
{
    Mock testListenerMock = this.buildMockForExceptionalCall();

    FakeListener fakeListener = (FakeListener) this.servlet.getListeners().getListener(LISTENER_NAME);
    fakeListener.setDelegate((JrpipEventListener) testListenerMock.proxy());

    Echo echo = this.buildEchoProxy();
    try
    {
        echo.throwUnexpectedException();
    }
    catch (RuntimeException e)
    {
        //caught test exception
    }

    testListenerMock.verify();
}
 
Example #2
Source File: JrpipListenerTest.java    From jrpip with Apache License 2.0 5 votes vote down vote up
private Mock buildMockForSuccessfulCall()
{
    Mock testListenerMock = new Mock(JrpipEventListener.class);

    testListenerMock.expects(this.once()).method("methodInvocationEvent").with(
            EventConstraint.forStartedEvent(EchoImpl.getEchoMethod(), new Object[]{"hello"}))
            .id("started event");

    testListenerMock.expects(this.once()).method("methodInvocationEvent").with(
            EventConstraint.forFinishedEvent(EchoImpl.getEchoMethod(), "hello"))
            .after("started event");

    return testListenerMock;
}
 
Example #3
Source File: JrpipListenerTest.java    From jrpip with Apache License 2.0 5 votes vote down vote up
private Mock buildMockForExceptionalCall()
{
    Mock testListenerMock = new Mock(JrpipEventListener.class);

    testListenerMock.expects(this.once()).method("methodInvocationEvent").with(
            EventConstraint.forStartedEvent(EchoImpl.getUnexpectedExceptionMethod(), new Object[]{}))
            .id("started event");

    testListenerMock.expects(this.once()).method("methodInvocationEvent").with(
            EventConstraint.forFailedEvent(EchoImpl.getUnexpectedExceptionMethod(), RuntimeException.class))
            .after("started event");

    return testListenerMock;
}
 
Example #4
Source File: JrpipListenerTest.java    From jrpip with Apache License 2.0 5 votes vote down vote up
public void testEchoEvents() throws MalformedURLException, InterruptedException
{
    Mock testListenerMock = this.buildMockForSuccessfulCall();

    FakeListener fakeListener = (FakeListener) this.servlet.getListeners().getListener(LISTENER_NAME);
    fakeListener.setDelegate((JrpipEventListener) testListenerMock.proxy());

    Echo echo = this.buildEchoProxy();
    Assert.assertEquals("hello", echo.echo("hello"));

    testListenerMock.verify();
}
 
Example #5
Source File: GroovyServletTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testRequestGetCommandOK() {
    Mock requestMock = mock(HttpServletRequest.class);
    requestMock.expects(once()).method("getParameter").with(eq("command")).will(returnValue("SELECT..."));
    HttpServletRequest request = (HttpServletRequest) requestMock.proxy();
    String command = request.getParameter("command");
    assertEquals("SELECT...", command);
}
 
Example #6
Source File: CompilationUnitTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
private GroovyClassLoader createGroovyClassLoaderWithExpectations(CompilerConfiguration configuration) {
    Mock mockGroovyClassLoader = mock(GroovyClassLoader.class);
    for (Iterator iterator = configuration.getClasspath().iterator(); iterator.hasNext();) {
        mockGroovyClassLoader.expects(once()).method("addClasspath").with(eq(iterator.next()));
    }
    return (GroovyClassLoader) mockGroovyClassLoader.proxy();
}
 
Example #7
Source File: InspectorTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void testInspectUninspectableProperty() {
    Object dummyInstance = new Object();
    Inspector inspector = getTestableInspector(dummyInstance);
    Class[] paramTypes = {Object.class, MetaProperty.class};
    Object[] params = {null, null};
    Mock mock = mock(PropertyValue.class, paramTypes, params);
    mock.expects(once()).method("getType");
    mock.expects(once()).method("getName");
    mock.expects(once()).method("getValue").will(throwException(new RuntimeException()));
    PropertyValue propertyValue = (PropertyValue) mock.proxy();
    String[] result = inspector.fieldInfo(propertyValue);
    assertEquals(Inspector.NOT_APPLICABLE, result[Inspector.MEMBER_VALUE_IDX]);
}
 
Example #8
Source File: PortletRequestImplTest.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
/**
 * Test for PLUTO-474.
 */
// TODO: adjust test to new container implementation, disabled for now
public void __testInvalidateSessionWithUnititializedLastAccessTime() throws Exception
{
    // maximum inactive interval of the underlying PortletRequest's HttpSession
    int maxInactiveInterval = 5; // in seconds

    // last accessed time of the underlying PortletRequest's HttpSession
    // A 'lastAccessedTime' of 0 emulates the behavior
    // of a servlet container that doesn't initialize
    // its value.
    long lastAccessedTime = 0L;  // in milliseconds
            
    Mock mockPortletEnvironmentService = mock( PortletEnvironmentService.class );
    
    mockServices.expects( once() ).method( "getPortletEnvironmentService" ).will( returnValue( mockPortletEnvironmentService.proxy() ));
    
    mockCCPPProfileService.expects(once()).method("getCCPPProfile").will(returnValue( null ));
    
    mockServices.expects(once()).method("getCCPPProfileService").will(returnValue( mockCCPPProfileService.proxy() ));
    
    mockContainer.expects(once()).method("getRequiredContainerServices").will(returnValue( mockServices.proxy() ));
    mockContainer.expects(atLeastOnce()).method("getOptionalContainerServices").will(returnValue( mockServices.proxy() ));
    mockPortletRequestContext.expects(atLeastOnce()).method("getContainer").will(returnValue( mockContainer.proxy()));
    
    // for the maximum inactive interval
    Mock mockHttpSession = mock( HttpSession.class );
    mockHttpSession.expects( once() ).method( "getLastAccessedTime" ).will( returnValue( lastAccessedTime ) );
    // Prior to applying PLUTO-474, this expectation is invoked exactly twice, not once
    mockHttpSession.expects( once() ).method( "getMaxInactiveInterval" ).will( returnValue( maxInactiveInterval ) );

    // Set the expectation for the servlet request - it will return the mock http session
    // Prior to applying PLUTO-474, this expectation is invoked exactly twice, not once
    mockHttpServletRequest.expects( once() ).method( "getSession" ).will( returnValue( mockHttpSession.proxy() ) );

    // this is the important expectation -
    // Prior to applying PLUTO-474, the HttpSession was
    // incorrectly determined to be invalid, and thus the
    // HttpSession's invalidate() method was invoked.
    //
    // After applying PLUTO-474, invalidate() should never be called
    mockHttpSession.expects( never() ).method( "invalidate" );
    
    Mock mockPortletSession = mock( PortletSession.class );
    mockPortletEnvironmentService.expects( once() ).method( "createPortletSession" ).will( returnValue( mockPortletSession.proxy() ));

}