- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用 Maven 进行注释处理期间使用 CodeModel 生成代码。该代码用于 JUnit 测试:
JMethod tearDownClass = testClass.method(
JMod.PUBLIC | JMod.STATIC, Void.class, "tearDownClass");
tearDownClass._throws(Exception.class);
tearDownClass.annotate(AfterClass.class); <- java.lang.NoClassDefFoundError
然而,当编译过程尝试检索 AfterClass.class
(它本身就是一个注释)时,它会抛出 java.lang.NoClassDefFoundError : org/junit/AfterClass
。
对 JUnit 的依赖定义如下:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
所以 AfterClass.class
应该在我的代码中可用。
如何解决这个问题?
不清楚
调用 codemodel 的代码位于编译库中,其中 junit 不是“测试”依赖项。但是,调用生成代码的代码具有相同的 junit 依赖项,但作为测试依赖项。
如果我将后一个依赖项更改为“非测试”依赖项,问题就会消失。为什么我必须将此依赖项定义为“非测试”,尽管只有调用代码模型的库明确使用它?
编辑
这是依赖树:
net.dwst:codegentest:jar:1.0.0
+- junit:junit:jar:4.8.2:compile
+- org.sonatype.maven.plugin:emma-maven-plugin:jar:1.2:test
| +- emma:emma:jar:2.0.5312:test
| \- org.apache.maven.reporting:maven-reporting-impl:jar:2.0.4:test
| +- commons-validator:commons-validator:jar:1.2.0:test
| | +- commons-beanutils:commons-beanutils:jar:1.7.0:test
| | +- commons-digester:commons-digester:jar:1.6:test
| | | \- commons-collections:commons-collections:jar:2.1:test
| | \- commons-logging:commons-logging:jar:1.0.4:test
| +- org.apache.maven.doxia:doxia-core:jar:1.0-alpha-7:test
| +- oro:oro:jar:2.0.7:test
| \- org.apache.maven.doxia:doxia-site-renderer:jar:1.0-alpha-7:test
| +- org.codehaus.plexus:plexus-i18n:jar:1.0-beta-6:test
| +- org.codehaus.plexus:plexus-velocity:jar:1.1.2:test
| | +- commons-logging:commons-logging-api:jar:1.0.4:test
| | \- velocity:velocity:jar:1.4:test
| | \- velocity:velocity-dep:jar:1.4:test
| \- org.apache.maven.doxia:doxia-decoration-model:jar:1.0-alpha-7:test
+- net.dwst:generics:jar:1.3.0:compile
| +- org.swinglabs:swing-layout:jar:1.0.3:compile
| \- com.sun.codemodel:codemodel:jar:2.4.1:compile
+- net.flat:flat:jar:1.3.0:compile
| \- com.pyx4me:proguard-maven-plugin:jar:2.0.4:compile
| +- ant:ant:jar:1.6.5:compile
| +- org.apache.maven:maven-archiver:jar:2.3:compile
| +- org.codehaus.plexus:plexus-archiver:jar:1.0-alpha-9:compile
| \- org.codehaus.plexus:plexus-io:jar:1.0-alpha-1:compile
+- org.codehaus.mojo:build-helper-maven-plugin:jar:1.7:compile
| +- org.apache.maven:maven-model:jar:2.0.6:compile
| +- org.apache.maven:maven-project:jar:2.0.6:compile
| | +- org.apache.maven:maven-settings:jar:2.0.6:compile
| | +- org.apache.maven:maven-profile:jar:2.0.6:compile
| | +- org.apache.maven:maven-artifact-manager:jar:2.0.6:compile
| | +- org.apache.maven:maven-plugin-registry:jar:2.0.6:compile
| | \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
| +- org.apache.maven:maven-core:jar:2.0.6:compile
| | +- org.apache.maven.wagon:wagon-file:jar:1.0-beta-2:runtime
| | +- org.apache.maven:maven-plugin-parameter-documenter:jar:2.0.6:compile
| | +- org.apache.maven.wagon:wagon-http-lightweight:jar:1.0-beta-2:runtime
| | | +- org.apache.maven.wagon:wagon-http-shared:jar:1.0-beta-2:runtime
| | | \- xml-apis:xml-apis:jar:1.0.b2:runtime
| | +- org.apache.maven.reporting:maven-reporting-api:jar:2.0.8:compile
| | | \- org.apache.maven.doxia:doxia-sink-api:jar:1.0-alpha-7:compile
| | +- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-2:compile
| | +- org.apache.maven:maven-repository-metadata:jar:2.0.6:compile
| | +- org.apache.maven:maven-error-diagnostics:jar:2.0.6:compile
| | +- commons-cli:commons-cli:jar:1.0:compile
| | +- org.apache.maven.wagon:wagon-ssh-external:jar:1.0-beta-2:runtime
| | | \- org.apache.maven.wagon:wagon-ssh-common:jar:1.0-beta-2:runtime
| | +- org.apache.maven:maven-plugin-descriptor:jar:2.0.6:compile
| | +- org.codehaus.plexus:plexus-interactivity-api:jar:1.0-alpha-4:compile
| | +- org.apache.maven:maven-monitor:jar:2.0.6:compile
| | +- org.apache.maven.wagon:wagon-ssh:jar:1.0-beta-2:runtime
| | | \- com.jcraft:jsch:jar:0.1.27:runtime
| | \- classworlds:classworlds:jar:1.1:compile
| +- org.apache.maven:maven-plugin-api:jar:2.0.6:compile
| +- org.apache.maven:maven-artifact:jar:2.0.6:compile
| \- org.codehaus.plexus:plexus-utils:jar:1.5.8:compile
\- org.bsc.maven:maven-processor-plugin:jar:2.0.3:compile
\- org.jfrog.maven.annomojo:maven-plugin-tools-anno:jar:1.4.0:compile
+- org.jfrog.maven.annomojo:maven-plugin-anno:jar:1.4.0:compile
+- org.apache.maven.plugin-tools:maven-plugin-tools-api:jar:2.6:compile
| \- jtidy:jtidy:jar:4aug2000r7-dev:compile
\- com.sun:tools:jar:1.5.0:system
很奇怪:
+- net.dwst:generics:jar:1.3.0:compile
| +- org.swinglabs:swing-layout:jar:1.0.3:compile
| \- com.sun.codemodel:codemodel:jar:2.4.1:compile
不包含org.junit
,是的,这个库的pom.xml有:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
...
最佳答案
如果我正确理解您的情况,您拥有的是两个库(A 和 B):
A是调用codemodel的代码。这依赖于 junit 4.8.2(编译范围)。
B 是调用 A 的代码(生成代码)。这依赖于 junit 4.8.2(测试范围)。
显然,B 依赖于 A。
来自Maven Dependency Scope ,我们有以下行,
Each of the scopes (except for import) affects transitive dependencies in different ways, as is demonstrated in the table below. If a dependency is set to the scope in the left column, transitive dependencies of that dependency with the scope across the top row will result in a dependency in the main project with the scope listed at the intersection. If no scope is listed, it means the dependency will be omitted.
传递依赖是来自直接依赖的依赖。所以库 X 依赖于库 Y。库 Y 依赖于库 Z。所以 Z 是库 X 的传递依赖。
在您的情况下,B 既直接依赖于 junit(具有测试范围),又通过 A 对 junit(具有编译范围)具有传递依赖性。如果我们阅读该表,我们可以看到这意味着优先的范围是test。这就是为什么您的代码无法找到 AfterClass.class,因为它不会被包含在内。
最好的选择是按照您已经尝试过的方式设置编译范围。
关于java - NoClassDefFoundError:注释处理期间的 org/junit/AfterClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7373105/
本着this question from JUnit 3 to JUnit 4的精神, 是否有任何正则表达式列表到 有效地从 junit 4 API 迁移到 junit 5 API ,不管代码大小?
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我需要以下测试 @runwith(cache, memory) class CollectionA is -- this is a suite (aka folder) class Cache {
当尝试在JUNITTEST的内存数据库中使用derby时,出现以下异常。 java.sql.SQLNonTransientConnectionException: Database 'memory:t
我需要以下测试 @runwith(cache, memory) class CollectionA is -- this is a suite (aka folder) class Cache {
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How to run Junit testcases from command line? 如何在 Linu
可以将 Junitperf 与 junit4 一起使用吗?我有一个带有多个测试的简单 Junit4 测试类,我想对该类的单个测试进行 TimedTest。我该怎么做? 更清楚地说,我的 Junit4
我想将 JUnit 4 测试添加到使用 JUnit 3 测试套件(“AllTests”)来组织测试的遗留项目中。 测试已经用 JUnit 4 执行了,所以我知道 JUnit 4 测试在原则上是有效的。
我正在将我的代码库从 junit4 迁移到 junit5。我在我的测试用例中使用了 mockito。下面是我用于依赖项的不同版本。 5.2.0 1.2.0 1.10.19 or
我刚刚使用 qunit-reporter-junit 生成了以下 XML: 但是当我运行它时,我在以下位置找到了 xsd:http
我已经编写了一个自定义 JUnit 运行器,我希望它成为 eclipse 插件的一部分,该插件将使用该运行器启动测试,而无需将 @RunWith 注释应用于该类。我已经设法使用 org.eclipse
我发现我的Sonar实例5.1或5.1.1(带有最新的声纳运行器2.x)停止在项目的仪表板上显示部分单元测试信息(单元测试小部件)。 我拥有的属性是(在Gradle的sonarRunner> sona
我有一个 JUnit 测试。但是当我使用“Run as -> JUnit”时它会成功,而当我使用“Cover as -> JUnit”时它会失败。这是为什么?代码确实有问题。在代码中,我使用了一些遗留
这个问题在这里已经有了答案: How to test code dependent on environment variables using JUnit? (20 个答案) 关闭 8 年前。 我
当我们的临时服务器因每周维护而停机时,我们有许多集成测试失败。当临时服务器关闭时,我们会发送一个特定的响应,我可以在集成测试中检测到该响应。当我得到这个响应而不是测试失败时,我想知道是否可以跳过/忽略
我需要测试一个程序,它首先预处理一些数据,然后使用这些预处理过的数据计算几个不同的结果——为每个计算编写单独的测试是有意义的。 官方 JUnit 政策似乎是我应该在每次计算测试之前运行预处理。 我如何
JUnit 是否可以为每个测试方法添加描述文本,以便描述文本稍后出现在surefire/failsave xml 报告中!? 背景:我在受监管的环境中工作,必须编写大量文档、测试规范和测试报告。 JU
当 JUnit 中的断言失败时,我想做一些“自己的事情”。我想要这个: public class MyAssert extends org.junit.Assert { // @Overrid
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我想将参数从运行配置传递给我的 JUnit 测试。我如何到达 JUnits 的主要方法来访问这些参数?有谁知道如何做到这一点? 谢谢 最佳答案 您可以使用 -D 系统属性运行单元测试,并使用 Syst
我是一名优秀的程序员,十分优秀!