简体   繁体   English

JUnit 5 掷方法未找到 Mockito.extension

[英]JUnit 5 throw method not found with Mockito.extension

I am updating a test from JUnit 4 to JUnit 5 (Jupiter).我正在将测试从 JUnit 4 更新到 JUnit 5(木星)。

Among the ordinary annotation adaptations such as @BeforeEach I am using @ExtendWith(MockitoExtension.class) to run the @mocks.@BeforeEach等普通注释改编中,我使用@ExtendWith(MockitoExtension.class)来运行@mocks。

The code is like this:代码是这样的:

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

    }

However when I run the test I am getting the following error:但是,当我运行测试时,出现以下错误:

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

I saw on this comment: https://stackoverflow.com/a/49655834/2182500 , that this error can be a consequence of having different versions of junit-jupiter-api as dependency in the projects POM and the one in the run scope used by mockito-junit-jupiter .我在此评论中看到: https://stackoverflow.com/a/49655834/2182500 ,此错误可能是由于在项目 POM 和运行 Z31A1FD140BE4BEF2D11E121EC9A18A8 中具有不同版本的junit-jupiter-api作为依赖项的结果由mockito-junit-jupiter使用。

So I guaranteed to import the same version dependency for Jupiter, but I am still seeing the same error.所以我保证为 Jupiter 导入相同的版本依赖,但我仍然看到同样的错误。

pom entries. pom条目。 at surefire level:在万无一失的水平:

<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>

at JUnit level:在 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>

Has anyone bumped into this before and can help?有没有人碰到过这个并且可以提供帮助? Thank you in advance.先感谢您。

Turns out this error was related to the Mockito dependencies versions.原来这个错误与 Mockito 依赖版本有关。 Thus, failling to initalize mocks.因此,未能初始化模拟。

The project I was working at had an old version of mockito-core artifact that did not have a related mockito-junit-jupiter version.我正在从事的项目有一个旧版本的mockito-core工件,它没有相关的mockito-junit-jupiter版本。 the project needs to maintain mockito-core due to Junit4 tests mock annotaions.由于 Junit4 测试模拟注释,该项目需要维护模拟mockito-core Problem was solved by updating version of mockito core and using same version for mockito-junit-jupiter.通过更新 mockito 内核版本并为 mockito-junit-jupiter 使用相同版本解决了问题。 Like this:像这样:

    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM