org.junit.Assert 本身的 JUnit NoClassDefFoundError

ilo*_*mbo 5 java junit unit-testing junit4 assertion

运行此代码时出现以下错误:(ColorMatrixgetarray()返回 float[])

74 public void testHueShift() {
75     ColorMatrix cm1 = new ColorMatrix();
76     ColorMatrix cm2 = hueShift(0f);
77     assertArrayEquals(cm1.getArray(), cm2.getArray(), 0f);
78 }
Run Code Online (Sandbox Code Playgroud)

错误:

 ----- begin exception -----
 java.lang.NoClassDefFoundError: org.junit.Assert
    at com.Common.lib.test.ColorFilterFactoryTest.testHueShift(ColorFilterFactoryTest.java:77)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
 ----- end exception -----
Run Code Online (Sandbox Code Playgroud)

但项目构建路径有它:

在此输入图像描述

编辑
这是测试类类路径 在此输入图像描述

这是被测试的类(它是一个库项目) 在此输入图像描述

Aar*_*lla 4

堆栈跟踪包含解决方案的提示:它包含junit.framework.TestCase(因此该类位于类路径上)但后来,它找不到org.junit.Assert.

运行测试时,类路径上看起来好像有 JUnit 3.8,但编译它们时,类路径上有 JUnit 4。

了解单元测试运行程序如何构建类路径并在那里修复它。

  • 如果两个 JAR 都在类路径上,那么如果第一个 JAR 错误,就会发生奇怪的事情。您可以通过使用 `Assert.assertArrayEquals()` 在 JUnit 3 测试中使用 JUnit 4 代码。如果这不起作用,因为它选择了 JUnit 3 的 `Assert`,请创建一个包装您要使用的方法的帮助程序类。 (2认同)