gpt4 book ai didi

java - Mockito.extension 未找到 JUnit 5 throw 方法

转载 作者:行者123 更新时间:2023-11-30 05:27:43 25 4
gpt4 key购买 nike

我正在将测试从 JUnit 4 更新到 JUnit 5 (Jupiter)。

普通注释改编如@BeforeEach我正在使用@ExtendWith(MockitoExtension.class)运行@mocks。

代码是这样的:

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

@ExtendWith(MockitoExtension.class)
public class SomeTest {

private static final String TEST = "test";

@Mock
RetailerService retailerService;
private Delivery delivery;

@BeforeEach
public void setUp() {
when(retailerService.getMessage(any(String.class))).thenReturn(TEST);
delivery = new Delivery(retailerService);
}

@Test
public void should_have_delivery() {
assertEquals(getExpectedDeliveriesDTOs(), delivery.toDtos());
}

}

但是,当我运行测试时,出现以下错误:

java.lang.NoSuchMethodError: org.mockito.session.MockitoSessionBuilder.initMocks([Ljava/lang/Object;)Lorg/mockito/session/MockitoSessionBuilder;

我看到这个评论:https://stackoverflow.com/a/49655834/2182500 ,该错误可能是 junit-jupiter-api 版本不同的结果作为项目 POM 中的依赖项以及 mockito-junit-jupiter 使用的运行范围中的依赖项.

所以我保证为 Jupiter 导入相同的版本依赖项,但我仍然看到相同的错误。

pom 条目。在万无一失的水平:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
<skipTests>true</skipTests>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

在 JUnit 级别:

 <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.17.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>

有人曾经遇到过这个问题并可以提供帮助吗?预先感谢您。

最佳答案

事实证明此错误与 Mockito 依赖项版本有关。因此,无法初始化模拟。

我正在从事的项目有一个旧版本的 mockito-core 工件,没有相关的 mockito-junit-jupiter 版本。由于 Junit4 测试模拟注释,该项目需要维护 mockito-core。通过更新mockito核心版本并使用相同版本的mockito-junit-jupiter解决了问题。像这样:

    <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>

关于java - Mockito.extension 未找到 JUnit 5 throw 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58217192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com