views:

88

answers:

1

Hi

Although I tried out all suggestions I found, I still can't get the most trivial JUnit test running. The error message basically repeats saying "junit.framework.AssertionFailedError: No tests found in project002.trivial.TestClassTest".

You may inspect a snapshot of my IDE or download the zipped project.

Here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    <modelVersion>4.0.0</modelVersion>
    <groupId>project002</groupId>
    <artifactId>project002</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>project002</name>
    <url>http://maven.apache.org&lt;/url&gt;
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <repositories>
        <repository>
            <id>EclipseLink Repo</id>
            <url>http://www.eclipse.org/downloads/download.php?r=1&amp;amp;nf=1&amp;amp;file=/rt/eclipselink/maven.repo&lt;/url&gt;
        </repository>
    </repositories>
    <dependencies>
         <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.7</version>
              <scope>test</scope>
          </dependency>  
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                 <configuration>
                    <junitArtifactName>org.junit:com.springsource.org.junit</junitArtifactName>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Here is the error message:

-------------------------------------------------------------------------------
Test set: project002.trivial.TestClassTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.078 sec <<< FAILURE!
warning(junit.framework.TestSuite$1)  Time elapsed: 0 sec  <<< FAILURE!
junit.framework.AssertionFailedError: No tests found in project002.trivial.TestClassTest
    at junit.framework.Assert.fail(Assert.java:47)
    at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    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.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
    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)
+4  A: 

There's something wrong with your testcase. When you extend Testcase, it will always be executed using Junit 3.x, thus you need to prefix your test method with "test".

If you want to use junit 4.x, remove "extends Testcase", then annotate your test method with @Test.

This one runs using Junit 3.x, and it will not work because your test method is not prefixed with "test":-

public class TestClassTest extends TestCase {

    @Test
    public void isThisReallyTrue() {
        assertTrue(true);
    }
}

This one will run in Junit 4.x:-

public class TestClassTest {

    @Test
    public void isThisReallyTrue() {
        assertTrue(true);
    }
}
limc
Thanks. It almost worked. isThisReallyTrue() was renamed to testIsThisReallyTrue() and since assertTrue() is a static method, I had to adopt it to Assert.assertTrue(). Now it works fine!
@erlord, with JUnit 4, there is no longer any need to extend from TestCase or prefix your method names with "test", as long as you have @Test
matt b
Hi Matt ... but it did *not* work for me without having the method name prefixed by "test" ...
@erlord, if you don't want to use Assert.assertTrue(), do a "import static" on Assert. Then you can retain assertTrue() instead of Assert.assertTrue(). Hmm... @matt is right, you don't really need to prefix with "test" if you already annotate the test method with @Test. That said, you will find some folks (like me) prefer to prefix the test methods with "test" for readability purpose.
limc
Please, if you could really be bothered downloading my testcase from http://drop.io/test4jerror20100710 and give it a try. I wonder where I go wrong. Thanks!
I just repeated the test case and I am afraid I must confirm what I said before: isThisReallyTrue() is *not* recognized, but changing it to testIsThisReallyTrue() actually works.