【问题标题】:JUnit 5 throw method not found with Mockito.extension在 Mockito.extension 中找不到 JUnit 5 throw 方法
【发布时间】:2019-10-03 10:27:34
【问题描述】:

我正在将测试从 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,此错误可能是由于在项目 POM 中具有不同版本的 junit-jupiter-api 作为依赖项以及在 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>

有没有人遇到过这个问题并可以提供帮助? 提前谢谢你。

【问题讨论】:

  • 你能分享你与junit和mockito相关的pom条目吗?
  • 嗨@camstastic 我将它编辑到帖子中。

标签: java intellij-idea junit mockito junit5


【解决方案1】:

原来这个错误与 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2012-09-13
    • 2018-03-10
    • 1970-01-01
    • 2015-03-21
    相关资源
    最近更新 更多