0
votes

I have the following JUnit test in eclipse:

package test;
import org.junit.Test;
public class SimpleJUnitTest
{
    @Test
    public void doTest() { System.out.println("Test did run"); }
}

And the following build.xml in the same folder:

<?xml version="1.0" encoding="UTF-8"?>
<project name="LoggerTest" default="JUnitTest" basedir=".">
    <target name="JUnitTest">
            <junit>
                <classpath location="../../lib/junit.jar" />
                <test name="test.SimpleJUnitTest" />
            </junit>
        <echo>boo</echo>
    </target>
</project>

If I run the test class under "Run As..." and choose JUnit, it runs without error. If I run the build.xml under "Run As..." and choose Ant Build, I get the following output:

Buildfile: C:\Users\995868\workspace\JUnit1\tst\test\build.xml
JUnitTest:
    [junit] Test test.SimpleJUnitTest FAILED
     [echo] boo BUILD SUCCESSFUL Total time: 390 milliseconds

If I remove the classpath attribute under JUnit, I get a different error message about needing the junit jar on the classpath, so I think JUnit is getting invoked. I just don't understand what its error is here. I've tried putting static block code in the class to do a System.out.println() when the class is loaded, and it does not appear, so there seems to be something I'm doing wrong in the configuration.

Can someone please tell me what's wrong here?

EDIT:

directory structure:
JUnit1
--bin
  --test
    --SimpleJUnitTest
--lib
  --junit.jar
--scripts
  --build.xml
--src
--tst
  --test
    --SimpleJUnitTest.java

I also copied build.xml to tst and ran it from the command line from that directory, same result.

I've copied junit.jar to %ant_home%\lib with no effect, though when I took the pathelement line out of the classpath I got the message "The for must include junit.jar if not in Ant's own classpath". I'm not sure where "Ant's own classpath" is specified. The classpath block with the new error message is this:

<classpath>
  <pathelement location="c:/users/995868/apache-ant-1.9.4/lib" />
  <pathelement location="../bin" />
</classpath>

I'm not using hamcrest features anywhere, so I haven't looked it up and put it in. I was trying to make a simple example, and the documentation for junit under ant (at least) does not mention that hamcrest is necessary.

2
Further info: I thought perhaps the test class itself could not be found; I copied build.xml to the bin directory, and verified that under bin was test\SimpleJUnitTest.class. I changed the classpath location since bin is in a different location relative to junit.jar. Since I cannot see the bin directory in eclipse, I just ran ant from a command line with the working directory set to bin. Same result.arcy

2 Answers

2
votes

I suppose you are missing hamcrest-core.jar from your ant classpath.

EDIT: and you are also missing the test file class itself from your classpath. So you need to update your classpath in the following manner:

<classpath>
  <pathelement location="bin"/> <!--here is your test class-->
  <pathelement location="lib/hamcrest-core-1.3.jar"/> 
  <pathelement location="lib/junit-4.12.jar"/> 
</classpath>
0
votes

Your class does not contain any testcases, which is an error. Your @Test annotation will be ignored, as you have not specified an @RunWith that will actually use it, so JUnit searches for methods named "test...".

So you have two choices:

a) Rename your method to "testSomething" or similar.

b) Add @RunWith(BlockJUnit4ClassRunner.class) above your class definition

Both ways will ensure that at least one test exists in your class, either via naming convention (a) or via annotion (b).