The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

asookazian (4) [Avatar] Offline
#1
The root cause is here as identified by the surefire-report:

@Test
public void testGetContentOk()
throws Exception
{
final ConnectionFactory factory = context.mock( ConnectionFactory.class );
final InputStream mockStream = context.mock( InputStream.class );

context.checking( new Expectations()
{
{
oneOf( factory ).getData();
will( returnValue( mockStream ) );

oneOf( mockStream ).read();
will( onConsecutiveCalls( returnValue( new Integer( (byte) 'W' ) ),
returnValue( new Integer( (byte) 'o' ) ),
returnValue( new Integer( (byte) 'r' ) ),
returnValue( new Integer( (byte) 'k' ) ),
returnValue( new Integer( (byte) 's' ) ),
returnValue( new Integer( (byte) '!' ) ),
returnValue( -1 ) ) );

oneOf( mockStream ).close();
}
} );

WebClient2 client = new WebClient2();

String result = client.getContent( factory );

assertEquals( "Works!", result );
}

Is this test supposed to build cleanly without any error? I have not modified any code after downloading the zip from the manning website for the book.
I am getting the following in the report:

-------------------------------------------------------------------------------
Test set: com.manning.junitbook.ch07.mocks.web.TestWebClientJMock
-------------------------------------------------------------------------------
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.092 sec <<< FAILURE!
testGetContentOk(com.manning.junitbook.ch07.mocks.web.TestWebClientJMock) Time elapsed: 0.049 sec <<< FAILURE!
java.lang.AssertionError: unexpected invocation: inputStream.read()
expectations:
expected once, already invoked 1 time: connectionFactory.getData(); returns <inputStream>
expected once, already invoked 1 time: inputStream.read(); returns <87>, and then returns <111>, and then returns <114>, and then returns <107>, and then returns <115>, and then returns <33>, and then returns <-1>
expected once, never invoked: inputStream.close(); returns a default value
what happened before this:
connectionFactory.getData()
inputStream.read()

at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
at org.jmock.Mockery.dispatch(Mockery.java:21smilie
at org.jmock.Mockery.access$000(Mockery.java:43)
at org.jmock.Mockery$MockObject.invoke(Mockery.java:25smilie
at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:3smilie
at org.jmock.lib.legacy.ClassImposteriser$4.invoke(ClassImposteriser.java:137)
at $java.io.InputStream$$EnhancerByCGLIB$$f8a9af0b.read(<generated>smilie
at com.manning.junitbook.ch07.mocks.web.WebClient2.getContent(WebClient2.java:52)
at com.manning.junitbook.ch07.mocks.web.TestWebClientJMock.testGetContentOk(TestWebClientJMock.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:37)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:105)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:9smilie
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:115)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:102)
at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)

C:javajunitbook2ch07-mocks>mvn -version
Apache Maven 3.0.3 (r1075438; 2011-02-28 09:31:09-0800)
Maven home: C:javaapache-maven-3.0.3
Java version: 1.6.0_24, vendor: Sun Microsystems Inc.
Java home: Csmilierogram FilesJavajdk1.6.0_24jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"