1

我正在使用 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$3(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.access$100(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>

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

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());
    }
}

4

1 回答 1

1

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

如果你想用 JUnit5 和 Mockito 运行测试,你的类应该是这样的(你想测试的对象被注释了@InjectMocks,它是你想要模拟的依赖@Mock项和类测试@ExtendWith(MockitoExtension.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());
    }
}
于 2020-05-27T07:41:47.887 回答