Java hamcrest 测试总是失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9651784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hamcrest tests always fail
提问by beachw08
I am using hamcrest 1.3 to test my code. It is simply a die. I am trying to test it to make sure the number generated is less than 13. I had a print statement that printed what the number generated was. The number generated was always less than 13 but the test always failed. Is there something I am doing wrong?
我正在使用 hamcrest 1.3 来测试我的代码。这简直就是一个死法。我正在尝试测试它以确保生成的数字小于 13。我有一个打印语句来打印生成的数字。生成的数字总是小于 13,但测试总是失败。有什么我做错了吗?
This is the code I am testing.
这是我正在测试的代码。
import java.util.Random;
public class Die {
private int numSides;
Random rand;
public Die(int numSides){
this.numSides = numSides;
rand = new Random(System.currentTimeMillis());
}
public int roll(){
return rand.nextInt(numSides) + 1;
}
}
And this is my test code.
这是我的测试代码。
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
public class DieTest {
@Test
public void testRoll() {
Die x = new Die(12);
assertThat(x.roll(), is(lessThan(13)));
}
}
Edit: This is the failure stack trace.
编辑:这是失败堆栈跟踪。
java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access##代码##0(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at DieTest.testRoll(DieTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access##代码##0(ParentRunner.java:42)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
采纳答案by beachw08
This is the site that help me solve the problem.
这是帮助我解决问题的网站。
http://code.google.com/p/hamcrest/issues/detail?id=128
http://code.google.com/p/hamcrest/issues/detail?id=128
The hamcrest.jar needs to go before the Junit library in the build path.
hamcrest.jar 需要位于构建路径中的 Junit 库之前。
回答by Kkkev
Use junit-dep.jar rather than junit.jar- this is JUnit minus its dependencies. Junit.jar contains an old version of Hamcrest.
使用 junit-dep.jar 而不是 junit.jar - 这是 JUnit 减去它的依赖项。Junit.jar 包含旧版本的 Hamcrest。
回答by Rade_303
In my Eclipse inside Project settings in Java Build Path section, Libraries I have previously added internal JUnit library which uses JUnit version 4.8 and hamcrest-core version 1.1. I believe that that was causing this error in my case.
在 Java Build Path 部分的项目设置中的 Eclipse 中,我之前添加了使用 JUnit 版本 4.8 和 hamcrest-core 版本 1.1 的内部 JUnit 库的库。我相信这在我的情况下导致了这个错误。
I leave this bit of information here, maybe somebody else would benefit from my experience.
我把这些信息留在这里,也许其他人会从我的经验中受益。
回答by Richard B
I had the same problem as detailed here. I believe the problem comes down to the junit4 jar file.
我遇到了与此处详述相同的问题。我相信问题归结为 junit4 jar 文件。
If, under eclipse pom editor, you look at the junit4 Hierarchy you will see that it has a dependency on hamcrest-core (i.e. hamcrest-core will, by default, be pulled in on compile). In my unit test code I use the hamcrest collection Matchers (org.hamcrest.collection). These aren't included in the core jar and I set up a dependency on hamcrest-all in the pom. Doing this duplicates the hamcrest-core inclusion and appear to leave you open to a version mismatch with the junit hamcrest-core dependency and hence the security exception. I removed the hamcrest-all dependency and replaced it with hamcrest-library and the exception went away.
如果在 eclipse pom 编辑器下查看 junit4 Hierarchy,您会发现它依赖于 hamcrest-core(即默认情况下,hamcrest-core 将在编译时被引入)。在我的单元测试代码中,我使用了 hamcrest 集合匹配器 (org.hamcrest.collection)。这些不包含在核心 jar 中,我在 pom 中设置了对 hamcrest-all 的依赖。这样做会复制 hamcrest-core 包含,并且似乎让您对与 junit hamcrest-core 依赖项的版本不匹配持开放态度,从而导致安全异常。我删除了 hamcrest-all 依赖项并将其替换为 hamcrest-library 并且异常消失了。
If you only use core hamcrest then you should not set up your own dependency and rely on the version junit pulls in. Alternately, as suggested in another comment, use junit-dep to strip out the junit dependency and then include hamcrest-all.
如果您只使用核心 hamcrest,那么您不应该设置自己的依赖项并依赖于 junit 引入的版本。或者,如另一条评论中所建议的,使用 junit-dep 去除 junit 依赖项,然后包含 hamcrest-all。
回答by John Mark
I was getting the same exception. Like beachw08 recommended, I referred to:
我得到了同样的例外。像beachw08推荐的,我指的是:
http://code.google.com/p/hamcrest/issues/detail?id=128
http://code.google.com/p/hamcrest/issues/detail?id=128
One of the posts said:
其中一个帖子说:
rename the file $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar to something like *.bak or remove the file.
将文件 $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar 重命名为 *.bak 之类的名称或删除该文件。
I did this and it solved my problem.
我这样做了,它解决了我的问题。
回答by Hunsu
I just removed JUnit library from my project configuration. I still can run the tests as JUnit is also included in my pom file. So the solution just use the library from Maven.
我刚刚从我的项目配置中删除了 JUnit 库。我仍然可以运行测试,因为 JUnit 也包含在我的 pom 文件中。所以解决方案只使用来自 Maven 的库。
回答by Jlearner
I resolved this problem by removing Junit4 library from the Build Path and added TestNG Library to the build path and imported TestNG annotations instead of Junit4 annotations in my java program.
我通过从构建路径中删除 Junit4 库并将 TestNG 库添加到构建路径并在我的 java 程序中导入 TestNG 注释而不是 Junit4 注释来解决这个问题。
回答by imbond
First of all make sure that you have added JUnit dependency in POM.xml file.
首先确保您已在 POM.xml 文件中添加了 JUnit 依赖项。
Now, right click on the project and go to properties, select Java build path and select Libraries tab.
现在,右键单击项目并转到属性,选择 Java 构建路径并选择库选项卡。
In my case there were Maven dependencies, JRE and Junit4 libraries. And I just removed Junit library and it works for me. Or one can also reorder the libraries as due to build order of Hamcrest and JUnit4 the problem was occurring.
就我而言,有 Maven 依赖项、JRE 和 Junit4 库。我刚刚删除了 Junit 库,它对我有用。或者,也可以根据 Hamcrest 和 JUnit4 的构建顺序对库重新排序,出现问题。
回答by Vaibhav Gupta
If you get the following exception "java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package", ensure that the hamcrest jar is before the Junit library in the build path. You can configure the order in the project properties under Java Build Path on the Order and Export tab. click below image link for more clarity : http://i.stack.imgur.com/Y5R15.png
如果得到如下异常“java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package”,确保hamcrest jar在Junit库之前构建路径。您可以在 Order and Export 选项卡上 Java Build Path 下的项目属性中配置顺序。单击下面的图片链接以获得更清晰的信息:http: //i.stack.imgur.com/Y5R15.png
回答by Ravi NAGALINGAM
i recently had this problem with eclipse and Junit.
我最近在使用 eclipse 和 Junit 时遇到了这个问题。
To solve this, i did that:
为了解决这个问题,我这样做了:
1 - Download the latest hamcrest-all jar from here : https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/hamcrest/
1 - 从这里下载最新的 hamcrest-all jar:https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/hamcrest/
2- Go on the eclipse installation folder: eclipse/plugin/ and find the org.hamcrest...jar
2- 进入 eclipse 安装文件夹:eclipse/plugin/ 并找到 org.hamcrest...jar
3- make a backup of the step 2 jar and replace it by the step 1 jar (rename it same as the jar step 2).
3- 备份第 2 步的 jar 并将其替换为第 1 步的 jar(将其重命名为与第 2 步的 jar 相同)。
4- restart eclipse
4-重启eclipse
After that, my issue was solved.
之后,我的问题就解决了。