tags:

views:

22

answers:

3

when i am runing junit test from ant i always get:

D:\metrike>ant test
Buildfile: build.xml

init:

compile:

test:
    [junit] Running jmt.test.TestCodeBase
    [junit] Testsuite: jmt.test.TestCodeBase
    [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0,046 sec
    [junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0,046 sec
    [junit]
    [junit] Testcase: warning(junit.framework.TestSuite$1):     FAILED
    [junit] No tests found in jmt.test.TestCodeBase
    [junit] junit.framework.AssertionFailedError: No tests found in jmt.test.TestCodeBase
    [junit]
    [junit]
    [junit] Test jmt.test.TestCodeBase FAILED

this is ant:

<target name="test" depends="compile">
        <mkdir dir="target/test-results"/>
        <junit haltonfailure="no" printsummary="on">
            <classpath >
                <pathelement location="target/classes"/>
                <pathelement location="Libraries/junit3.8.1/junit.jar"/>
            </classpath>
            <formatter type="brief" usefile="false"/>
            <formatter type="xml" />
            <batchtest todir="target/test-results" >
                <fileset dir="target/classes" includes="**/TestCodeBase.class"/>
            </batchtest>
        </junit>
    </target>

but when i manually run test junit test works:

D:\metrike>cd target

D:\metrike\target>cd classes

D:\metrike\target\classes>java jmt.test.TestCodeBase
fatsource.jar eclapsed : 2297 ms
over all : 2297 ms
contains 3073 classes and 3700 referred classes, 35968 referred methods, 22351 referred fields
Memory usage: 21326 KB
Post gc-memory usage: 19506 KB
contains 3073 classes and 3700 referred classes, 35968 referred methods, 22351 referred fields

can someone please tell me what i am doing wrong? i am trying to fix this for whole day but i can not find the solution. Thank you.

+1  A: 

1) Does jmt.test.TestCodeBase extend TestCase (junit.framework.TestCase)?

If not, it will need to to be picked up by the junit ant task.

2) Is the class written as a junit TestCase, or is it just called from the main method? See this link for an example of writing simple tests in Junit3 style. For Junit4, just add @Test above the methods.

3) Are the test methods in Junit3 style (every method starts with test) or Junit4 style (every method has a @Test above it)?

If Junit3, you should be good to go. If Junit4, you need to include the Junit4 test library in your ant classpath, rather than using junit3.8.1.

Steve Jackson
thx for help. Forget to extend TestCase
A: 

Please post sample code of jmt.test.TestCodeBase, esp. class definition and one of the test methods.

I looks like you use public static int main() instead of JUnit convention public void testFoo() methods. If you're using JUnit4, your test methods should have the @Test annotation.

JUnit tests usually cannot be run just with java <Testclass>

mhaller
thx for help. Forget to extend TestCase
+1  A: 

It seems your test class is not actually a JUnit test class. When you run it manually, you are not running it as a test, but as a regular Java application. The class has a main method, right? To run as a JUnit 3 (which you seem to be using) test, the class needs to extend TestCase and have one or more public void methods whose names start with 'test'. For testing, I would try running the class as a JUnit test in the IDE.

Fabian Steeg
thx for help. Forget to extend TestCase