2010-07-12 78 views
1

雖然我嘗試了所有我發現的建議,但仍然無法獲得最平常的JUnit測試運行。該錯誤消息基本上重複說「junit.framework.AssertionFailedError:沒有在project002.trivial.TestClassTest中找到測試」。您可以檢查a snapshot of my IDEdownload the zipped projectJUnit 4.8.1/Maven平凡測試不起作用

這裏是我的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"> 
    <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</url> 
    <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;nf=1&amp;file=/rt/eclipselink/maven.repo</url> 
     </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> 

以下是錯誤消息:

------------------------------------------------------------------------------- 
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) 

回答

10

有一些問題與您的測試用例。當您擴展Testcase時,它將始終使用Junit 3.x執行,因此您需要在測試方法前加上「test」。

如果您想使用junit 4.x,請刪除「extends Testcase」,然後使用@Test註釋您的測試方法。

這一個運行使用JUnit 3.x和它不會工作,因爲你的測試方法不以 「test」 爲前綴: -

public class TestClassTest extends TestCase { 

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

這人會在JUnit 4.x的運行: -

public class TestClassTest { 

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

謝謝。它幾乎奏效。 isThisReallyTrue()被重命名爲testIsThisReallyTrue(),因爲assertTrue()是一個靜態方法,所以我不得不採用它來Assert.assertTrue()。現在它工作正常! – 2010-07-12 15:16:50

+0

@erlord,使用JUnit 4,不再需要從TestCase擴展或在「測試」前添加方法名稱,只要您有@Test – 2010-07-12 16:16:52

+0

嗨馬特......但它確實*沒有*爲我工作如果你不想使用Assert.assertTrue(),則在Assert上做一個「導入靜態」,如果沒有前綴爲「test」的方法名稱... – 2010-07-12 19:49:40