Running “pure” JUnit 4 tests using ant

后端 未结 9 707
一个人的身影
一个人的身影 2020-12-25 13:05

We have migrated to both JUnit 4 and ant 1.7

The tests runs fine in eclipse, but the annotations are ignored when running the tests using ant.

According to t

相关标签:
9条回答
  • 2020-12-25 13:33

    This happened to me and it was because I was both using annotations and extending TestCase.

    public class TestXXX extends TestCase {
    
        @Test
        public void testSimpleValidCase() {
            // this was running
        }
    
        @Test
        public void simpleValidCase() {
            // this wasn't running
        }
    }
    

    When you extend TestCase you are assuming JUnit3 style so JUnit4 annotations are ignored.

    The solution is to stop extending TestCase.

    0 讨论(0)
  • 2020-12-25 13:33

    What I ended up doing was adding an Ant to one of my definitions that is used by the task>. Et voila.

    0 讨论(0)
  • 2020-12-25 13:34

    I also tried to do tests with JUnit 4.0 without JUnit4TestAdapter, i.e. without method
    public static junit.framework.Test suite() { return new JUnit4TestAdapter(SomeTestClass.class); }

    I use ant 1.9.4. Running ant test verbose (ant -v ) shows

    [junit] Running multiple tests in the same VM
    [junit] Implicitly adding /usr/share/java/junit.jar:/usr/sharejava/ant-launcher.jar:/usr/share/java/ant.jar:/usr/share/java/ant/ant-junit.jar to CLASSPATH

    Aha, but still there is some ant-junit-task. Downloading this shows in addition /usr/share/java/ant/ant-junit4.jar which is not added implicitly. I just added it explicitly:

    <junit printsummary="yes" 
        fork="yes" 
        forkmode="once"
        maxmemory="1023m" 
        showoutput="no">
        ...
       <classpath>
         <pathelement path="...:${junitJar}:${hamcrestJar}:/usr/share/java/ant/ant-junit4.jar" />
       </classpath>
    ...
    </junit>
    

    and it worked. Without: no. I am aware that this solution is not beautiful at all...

    0 讨论(0)
提交回复
热议问题