无法 运行 个单元测试用例

Unable to run unit test cases

我正在使用 Junit5 为我的项目编写测试用例,当我 运行 一个测试用例时,所有测试用例都通过了,但其中一些在控制台中给我以下错误

WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoClassDefFoundError: junit/framework/ComparisonFailure
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestListener.getFailedComparison(JUnit5TestListener.java:133)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestListener.notifyIfNotSuccessful(JUnit5TestListener.java:89)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestListener.executionFinished(JUnit5TestListener.java:71)
    at org.junit.platform.launcher.core.TestExecutionListenerRegistry$CompositeTestExecutionListener.lambda$executionFinished(TestExecutionListenerRegistry.java:77)
    at java.util.ArrayList.forEach(Unknown Source)
    at org.junit.platform.launcher.core.TestExecutionListenerRegistry.notifyTestExecutionListeners(TestExecutionListenerRegistry.java:51)
    at org.junit.platform.launcher.core.TestExecutionListenerRegistry.access0(TestExecutionListenerRegistry.java:27)
    at org.junit.platform.launcher.core.TestExecutionListenerRegistry$CompositeTestExecutionListener.executionFinished(TestExecutionListenerRegistry.java:77)
    at org.junit.platform.launcher.core.ExecutionListenerAdapter.executionFinished(ExecutionListenerAdapter.java:56)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.reportCompletion(NodeTestTask.java:138)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:79)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

例如

Mockito.verify(exportFile, Mockito.times(1)).exportCsvFile(Mockito.anyList(), Mockito.anyString());
Mockito.verify(exportFile, Mockito.times(2)).exportCsvFile(Mockito.anyList(), Mockito.anyString());
Mockito.verify(exportFile, Mockito.times(3)).exportCsvFile(Mockito.anyList(), Mockito.anyString();

每次测试用例都通过,但我看到的是第二个和第三个用例 java.lang.NoClassDefFoundError: junit/framework/ComparisonFailure
下面是我的 pom 的样子:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <!-- exclude junit 4 -->
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>

下面是我的测试 class 的样子(提供 class 的必要部分)。

public class AccountServiceTest {
    AccountService AccountService;
    @Mock
    ExportFile exportFile;

    @BeforeEach
    public void init() {
        MockitoAnnotations.initMocks(this);
        accountService = new AccountService(exportFile);
    }
    @Test
    public void testFileExport() {
        accountService.process(partner.getName());
        Mockito.verify(exportFile).exportCsvFile(Mockito.anyList(), Mockito.anyString());
    }
}

如果您可以将 spring-boot-starter-test 升级到至少 2.2.0.RELEASE,它将为您带来 JUnit 5(如果您想要 运行,也不需要排除JUnit4 和 JUnit5 同时测试)。仅使用 spring boot starter 依赖项是处理依赖项之间不兼容版本问题的好方法,因为 spring boot 会为您管理。

如果你想 运行 使用 JUnit5 和 Mockito 进行测试,你的 class 应该看起来像这样(你要测试的对象用 @InjectMocks 注释并且它是你想要的依赖项用 @Mock 模拟,用 @ExtendWith(MockitoExtension.class) 测试 class) :

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class AccountServiceTest {

    @Mock
    ExportFile exportFile;

    @InjectMocks
    AccountService accountService;

    @Test
    public void testFileExport() {
        accountService.process("");
        Mockito.verify(exportFile).exportCsvFile(Mockito.anyList(), Mockito.anyString());
    }
}