- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我刚刚创建了一个虚拟 Maven 项目:
<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>com.loki2302</groupId>
<artifactId>junit-test-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>junit-test-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
我在这个项目中只有一个测试:
package com.loki2302;
import java.util.Arrays;
import java.util.List;
import org.junit.Test; // for @Test annotation
import static org.junit.Assert.*; // for assertThat()
import static org.junit.matchers.JUnitMatchers.*; // for hasItem()
public class AppTest {
@Test
public void sillyTest() {
List<Integer> list = Arrays.asList(123, 456);
assertThat(list, hasItem(123));
}
}
当我运行 mvn clean test
时,它说:
D:\junit-test-app>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-test-app 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ junit-test-app ---
[INFO] Deleting D:\junit-test-app\target
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ junit-test-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\junit-test-app\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ junit-test-app ---
[INFO] Compiling 1 source file to D:\junit-test-app\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ junit-test-app ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\junit-test-app\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ junit-test-app ---
[INFO] Compiling 1 source file to D:\junit-test-app\target\test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \junit-test-app\src\test\java\com\loki2302\AppTest.java:[15,19] error: cannot find symbol
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.512s
[INFO] Finished at: Wed Jul 11 10:38:06 MSD 2012
[INFO] Final Memory: 12M/107M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project junit-test-app: Compilation failure
[ERROR] \junit-test-app\src\test\java\com\loki2302\AppTest.java:[15,19] error: cannot find symbol
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Maven 版本:
D:\junit-test-app>mvn --version
Apache Maven 3.0.3 (r1075438; 2011-02-28 20:31:09+0300)
Maven home: D:\apache-maven-3.0.3\bin\..
Java version: 1.7.0, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0\jre
Default locale: en_US, platform encoding: Cp1251
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
Java 版本:
D:\junit-test-app>java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) 64-Bit Server VM (build 21.0-b17, mixed mode)
如何让它发挥作用?
更新
修复了 Assert
和 JUnitMatchers
的导入,更新了 mvn clean test
的输出
最佳答案
你有:
import org.junit.Assert.*;
import org.junit.matchers.JUnitMatchers.*;
当你想要的是:
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;
关于java - 无法使 JUnit 工作(assertThat/hasItem),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11426758/
我看到了这个post assertThat( myClass.getMyItems(), contains( hasProperty("foo", is("bar")), hasPro
我有一个整数列表(当前),我想检查这个列表是否包含预期列表中的所有元素,甚至不包含列表 notExpected 中的一个元素,所以代码如下: List expected= new ArrayL
我已经绝望了,我不明白为什么这个测试没有被评估为成功。我已经检查过一百万次了: package someptest; import static org.hamcrest.MatcherAssert.
我遇到了 hamcrest 和mockito 的问题。这是我正在尝试做的事情: public class A{ public void foo(List arg){ re
我想测试我的 Controller 并使用以下方法来测试它: package spittr.web; import static org.springframework.test.web.servle
如何使用 Hamcrest 中的 TestNG 和 hasItem 匹配空集合?这是我通过一项测试得到的结果。 java.lang.AssertionError: Expected: a collec
我目前正在测试 hasItem() Matcher 但无济于事。请参阅下面的示例代码: List list = new ArrayList(); list.add("1"); list.add("2"
我刚刚创建了一个虚拟 Maven 项目: 4.0.0 com.loki2302 junit-test-app 0.0.1-SNAPSHOT jar j
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; im
为什么编译不出来啊,怎么办? import static org.junit.Assert.assertThat; import static org.junit.matchers.JUnitMatc
我看到了这个post 关于两者的区别: Matchers.hasItem(..) Assert.assertThat(items, Matchers.hasItem(Matchers.hasToStr
我想使用 Hamcrest 的 hasItems带有一个“实际”集合,即 ArrayList在 assertThat(ArrayList, hasItems(InstanceOfSomeInterfa
Hamcrest 提供了许多匹配器来断言集合的内容。所有这些情况都通过: Collection c = ImmutableList.of("one", "two", "three"); assertT
我是一名优秀的程序员,十分优秀!