PowerMock Mockito [PowerMockito] @PrepareForTest -> java.lang.NoClassDefFoundError: javassist/NotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23157279/
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
PowerMock Mockito [PowerMockito] @PrepareForTest -> java.lang.NoClassDefFoundError: javassist/NotFoundException
提问by cellepo
I'm trying to use my 1st PowerMockito [PowerMock + Mockito] mock in an existing JUnit test.
我正在尝试在现有的 JUnit 测试中使用我的第一个 PowerMockito [PowerMock + Mockito] 模拟。
I've narrowed down that when I simply have the presence of the class-level annotation @PrepareForTest
(along with @RunWith(PowerMockRunner.class)
, but not any actual mocking code), I get java.lang.NoClassDefFoundError: javassist/NotFoundException
: If I comment out the @PrepareForTest
, and also any actual mocking code to eliminate confounding variables, that error does not happen; if I UN-comment just the @PrepareForTest
, then the error happens. So I believe that narrows the problem down to just to do with trying to use the annotation...
我已经缩小了范围,当我只存在类级注释@PrepareForTest
(以及@RunWith(PowerMockRunner.class)
,但没有任何实际的模拟代码)时,我得到java.lang.NoClassDefFoundError: javassist/NotFoundException
:如果我注释掉@PrepareForTest
,以及任何实际的模拟代码以消除混淆变量,该错误不会发生;如果我仅对 进行 UN 注释@PrepareForTest
,则会发生错误。所以我相信这将问题缩小到仅与尝试使用注释有关......
I have these jars on my buildpath: junit-4.11.jaar, junit-addons-1.4.jar, mockito-all-1.9.5.jar, mockito-all-1.9.5-sources.jar, powermock-mockito-1.5.4-full.jar.
我的构建路径中有这些 jar:junit-4.11.jaar、junit-addons-1.4.jar、mockito-all-1.9.5.jar、mockito-all-1.9.5-sources.jar、powermock-mockito-1.5。 4-full.jar。
The powermock-mockito-1.5.4-full.jar is the only new one I added to try out PowerMockito (from https://code.google.com/p/powermock/wiki/Downloads?tm=2): It gives me everything additional I need to write the new PowerMockito mocking code without Eclipse compile errors. (I already had the other mentioned ars from previous existing JUnit & Mockito [alone, no PowerMockito] tests),
powermock-mockito-1.5.4-full.jar 是我为试用 PowerMockito 添加的唯一一个新文件(来自https://code.google.com/p/powermock/wiki/Downloads?tm=2):它提供我需要在没有 Eclipse 编译错误的情况下编写新的 PowerMockito 模拟代码所需的一切。(我已经从以前的现有 JUnit 和 Mockito [单独,没有 PowerMockito] 测试中获得了其他提到的 ars),
What could I be doing wrong to get that error?
我可能做错了什么才能得到那个错误?
采纳答案by cellepo
(Thispretty much helped me figure it out)
(这几乎帮助我弄清楚了)
Pretty simple solution (I almost had it right to begin with): I was missing a few PowerMockito dependency jars(from https://code.google.com/p/powermock/wiki/Downloads?tm=2): Specifically {cglib-nodep-2.2.2.jar, javssist-3.18.1-GA.jar, objenesis-2.1.jar}.
非常简单的解决方案(我几乎是正确的开始):我错过了一些 PowerMockito 依赖 jar(来自https://code.google.com/p/powermock/wiki/Downloads?tm=2):特别是 {cglib -nodep-2.2.2.jar、javssist-3.18.1-GA.jar、objenesis-2.1.jar}。
It was a simple solution and kinda dumb mistake to begin with in the 1st place, since that powerMock googleCode download page has all of them: At 1st, when I had the problem, I had only downloaded and added powermock-mockito-1.5.4-full.jar; I already had the JUnit & Mockito jars, so I think that set of 3 let me write PowerMock code without any compile errors in Eclipse. The googlecode download page has the powermockito jar + dependecies in a zip, but also said powerockito jar available alone too. That situation made it kinda hard to realize I was missing some others since it appeared I had all I need from that perspective (thought I already had the dependencies I needed per my existing JUnit and Mockito jars).
这是一个简单的解决方案,并且一开始就有点愚蠢的错误,因为那个 powerMock googleCode 下载页面包含所有这些: 第一,当我遇到问题时,我只下载并添加了 powermock-mockito-1.5.4 -full.jar; 我已经有了 JUnit 和 Mockito jar,所以我认为这 3 个 jar 集让我可以在 Eclipse 中编写 PowerMock 代码而不会出现任何编译错误。googlecode 下载页面在 zip 中有 powermockito jar + 依赖项,但也说 powerockito jar 也可以单独使用。这种情况让我很难意识到我错过了一些其他人,因为从这个角度来看,我似乎拥有了我需要的一切(以为我已经有了我现有的 JUnit 和 Mockito jar 所需的依赖项)。
In actuality, the zip with dependencies had the additional ones I needed to solve the problem. I should have double-checked that 1st, but didn't realize I made the opposite bad assumption.
实际上,具有依赖项的 zip 具有解决问题所需的附加 zip。我应该仔细检查第一次,但没有意识到我做了相反的错误假设。
So... answer is to make sure to get the zip with all dependencies [and add them all to your buildpath]. Kinda obvious and dumb in my part in the end, but hey, a lot of software bugs and problems end up being that: Simple bad assumptions or overlooked details we don't realize we made, and therefore take a while to realize when troubleshooting...
所以......答案是确保获得包含所有依赖项的 zip [并将它们全部添加到您的构建路径]。对我来说,最终有点明显和愚蠢,但是嘿,很多软件错误和问题最终都是这样的:简单的错误假设或我们没有意识到我们所做的被忽视的细节,因此在进行故障排除时需要一段时间才能意识到。 ..
回答by Vicky
I was lately trying to run Mockito examples and failing to supply all the Jar files.
我最近尝试运行 Mockito 示例,但未能提供所有 Jar 文件。
This link contains links for necessary binaries.
此链接包含必要二进制文件的链接。
https://github.com/jayway/powermock/wiki/Downloads
https://github.com/jayway/powermock/wiki/Downloads
Use below for Mockito and PowerMock combination.
使用下面的 Mockito 和 PowerMock 组合。
powermock-mockito-junit-1.6.3.jar
powermock-mockito-junit-1.6.3.jar
回答by johnm
We were getting this with one of our ant junit targets that was using PowerMock 1.5.5. The issue, in our case, was the wrong version of javassist was on the ant junit task classpath.
我们通过使用 PowerMock 1.5.5 的 ant junit 目标之一获得了这一点。在我们的例子中,问题是 ant junit 任务类路径上的 javassist 版本错误。
PowerMock 1.5.5, according to this linkfrom the mvn rep, has a dependency on javassist 3.18.2-GA
根据mvn rep 的链接,PowerMock 1.5.5依赖于javassist 3.18.2-GA
Once we fixed this.. all our mocking fun began!
一旦我们解决了这个问题……我们所有的嘲笑就开始了!