Java JUnit 4.8.1/Maven琐碎测试不工作

Java JUnit 4.8.1/Maven琐碎测试不工作,java,unit-testing,maven-2,junit,Java,Unit Testing,Maven 2,Junit,尽管我尝试了我找到的所有建议,但我仍然无法运行最简单的JUnit测试。错误消息基本上重复说“junit.framework.AssertionFailedError:在project002.Trial.TestClassTest中找不到测试” 你可以检查或检查 以下是我的pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc

尽管我尝试了我找到的所有建议,但我仍然无法运行最简单的JUnit测试。错误消息基本上重复说“junit.framework.AssertionFailedError:在project002.Trial.TestClassTest中找不到测试”

你可以检查或检查

以下是我的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>

4.0.0
项目002
项目002
1
罐子
项目002
http://maven.apache.org
UTF-8
日食回购
http://www.eclipse.org/downloads/download.php?r=1&nf=1&;文件=/rt/eclipselink/maven.repo
朱尼特
朱尼特
4.7
测试
org.apache.maven.plugins
maven编译器插件
2.3.1
1.6
1.6
UTF-8
org.apache.maven.plugins
maven surefire插件
2.5
org.junit:com.springsource.org.junit
**/*Test.java
以下是错误消息:

-------------------------------------------------------------------------------
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)
-------------------------------------------------------------------------------
测试集:project002.triple.TestClassTest
-------------------------------------------------------------------------------

测试运行:1,失败:1,错误:0,跳过:0,运行时间:0.078秒您的测试用例有问题。当您扩展Testcase时,它将始终使用JUnit3.x执行,因此您需要在您的测试方法前面加上“test”

如果您想使用JUnit4.x,请删除“extenstedtestcase”,然后用@test注释您的测试方法

这一个使用Junit 3.x运行,它将不起作用,因为您的测试方法没有前缀“test”:-

这个将在Junit 4.x中运行:-

public class TestClassTest {

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

您的测试用例有问题。当您扩展Testcase时,它将始终使用JUnit3.x执行,因此您需要在您的测试方法前面加上“test”

如果您想使用JUnit4.x,请删除“extenstedtestcase”,然后用@test注释您的测试方法

这一个使用Junit 3.x运行,它将不起作用,因为您的测试方法没有前缀“test”:-

这个将在Junit 4.x中运行:-

public class TestClassTest {

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

谢谢它几乎成功了。isThisReallyTrue()被重命名为testIsThisReallyTrue(),由于assertTrue()是一个静态方法,我不得不采用它来Assert.assertTrue()。现在它工作得很好@erlord,在JUnit4中,不再需要从TestCase扩展或在方法名前面加上“test”,只要您有@TestHi Matt。。。但是,如果方法名的前缀没有“test”…@erord,那么它对我来说就不起作用了。如果您不想使用Assert.assertTrue(),请在Assert上执行“import static”。然后可以保留assertTrue()而不是Assert.assertTrue()。嗯@matt是对的,如果您已经用@test注释了测试方法,那么您实际上不需要使用“test”作为前缀。也就是说,你会发现一些人(像我一样)为了可读性,更喜欢在测试方法前面加上“test”。我不知道我哪里出错了。谢谢谢谢它几乎成功了。isThisReallyTrue()被重命名为testIsThisReallyTrue(),由于assertTrue()是一个静态方法,我不得不采用它来Assert.assertTrue()。现在它工作得很好@erlord,在JUnit4中,不再需要从TestCase扩展或在方法名前面加上“test”,只要您有@TestHi Matt。。。但是,如果方法名的前缀没有“test”…@erord,那么它对我来说就不起作用了。如果您不想使用Assert.assertTrue(),请在Assert上执行“import static”。然后可以保留assertTrue()而不是Assert.assertTrue()。嗯@matt是对的,如果您已经用@test注释了测试方法,那么您实际上不需要使用“test”作为前缀。也就是说,你会发现一些人(像我一样)为了可读性,更喜欢在测试方法前面加上“test”。我不知道我哪里出错了。谢谢