Java源码示例:org.jmock.Mock
示例1
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();
}
示例2
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;
}
示例3
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;
}
示例4
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();
}
示例5
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);
}
示例6
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();
}
示例7
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]);
}
示例8
/**
* 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() ));
}