JUnit test that uses ExpectIt fails when run by Ant

17 views
Skip to first unread message

randy....@gmail.com

unread,
Jun 3, 2017, 9:48:55 PM6/3/17
to Yet another Expect for Java
When I use Expect in my JUnit tests and run them with java org.junit.runner.JUnitCore, they work fine! Yea!

When I use Expect in my JUnit tests and run them with ant -- they fail miserably in the setUp() (@before) methods.

Here is the body of my setUp() method (as I said, it works using the junit runner):

ProcessBuilder pb = new ProcessBuilder("python","-u", "tictactoe.py");
process = pb.start();

expect = new ExpectBuilder()
.withTimeout(1000, TimeUnit.MILLISECONDS)
.withInputs(process.getInputStream(), process.getErrorStream())
.withOutput(process.getOutputStream())
.build();

StringBuilder sb = new StringBuilder();
sb.append("This is the current board: " + System.lineSeparator());
sb.append("[[0, 0, 0]," + System.lineSeparator());
sb.append(" [0, 0, 0]," + System.lineSeparator());
sb.append(" [0, 0, 0]]" + System.lineSeparator());
sb.append("Choose where to play. Enter two numbers seperated by a comma, for example: 1,1 ");
initString = sb.toString();

try{
boolean result = expect.expect(exact(initString)).isSuccessful();
assertTrue(result);
} catch (IOException e) {
fail(ExceptionUtils.getStackTrace(e));
}

Here is my ant JUnit target:
<target name="test" depends="test-compile">
<junit printsummary="on" haltonfailure="no">
<classpath>
<path refid="classpath.test"/>
<pathelement location="${test.build.dir}"/>
</classpath>
<formatter type="xml" />
<formatter type="plain" />
<batchtest>
<fileset dir="${test.src.dir}" includes="*Tests.java" />
</batchtest>
</junit>
</target>

Here is the exception:

java.io.EOFException: Input closed
at net.sf.expectit.SingleInputExpect.expect(SingleInputExpect.java:167)
at net.sf.expectit.ExpectImpl.expectIn(ExpectImpl.java:103)
at net.sf.expectit.AbstractExpectImpl.expectIn(AbstractExpectImpl.java:57)
at net.sf.expectit.AbstractExpectImpl.expect(AbstractExpectImpl.java:61)
at TicTacToeTests.setUp(TicTacToeTests.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:38)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:535)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:1647)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:1047)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:2207)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:858)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:435)
at org.apache.tools.ant.Target.performTasks(Target.java:456)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
at org.apache.tools.ant.Main.runBuild(Main.java:857)
at org.apache.tools.ant.Main.startAnt(Main.java:236)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113)

junit.framework.AssertionFailedError: java.io.EOFException: Input closed
at net.sf.expectit.SingleInputExpect.expect(SingleInputExpect.java:167)
at net.sf.expectit.ExpectImpl.expectIn(ExpectImpl.java:103)
at net.sf.expectit.AbstractExpectImpl.expectIn(AbstractExpectImpl.java:57)
at net.sf.expectit.AbstractExpectImpl.expect(AbstractExpectImpl.java:61)
at TicTacToeTests.setUp(TicTacToeTests.java:43)

at TicTacToeTests.setUp(TicTacToeTests.java:46)

It seems as though ant is closing the streams in my setUp() method for some reason. Any hints as to how to modify my ant JUnit target so it won't do that?
fork true false on off doesn't help.

Alexey Gavrilov

unread,
Jun 4, 2017, 4:11:14 AM6/4/17
to randy....@gmail.com, Yet another Expect for Java
Th difference between running from Ant and IDE can be the current directory. Try to use the absolute path to the python script.
> --
> You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

randy....@gmail.com

unread,
Jun 4, 2017, 2:48:06 PM6/4/17
to Yet another Expect for Java, randy....@gmail.com, Alexey1....@gmail.com
Dood you are so right. I moved my script out of the bin folder and into the project directory -- works fine now.
Reply all
Reply to author
Forward
0 new messages