【问题标题】:Getting Mockito can only mock non-private & non-final classes error while running test cases获取 Mockito 只能在运行测试用例时模拟非私有和非最终类错误
【发布时间】:2020-09-05 11:04:57
【问题描述】:

我正在使用 Java 11。几乎尝试了所有方法,但仍然无法正常工作。也完成了使缓存无效并重新启动。

[![在此处输入图片描述][1]][1]

这是我在 pom.xml 中添加的依赖项

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>*emphasized text*

这是模拟的类,它不能模拟,我有一些 UserService 的测试用例。

这是 UserServiceImpltest.java

package com.stackroute.keepnote.test.service;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.stackroute.keepnote.exceptions.UserNotFoundException;
import com.stackroute.keepnote.service.UserServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.stackroute.keepnote.exceptions.UserAlreadyExistsException;
import com.stackroute.keepnote.model.User;
import com.stackroute.keepnote.repository.UserRepository;



public class UserServiceImplTest {

    @Mock
    UserRepository userRepository;


    User user;

    @InjectMocks
    UserServiceImpl userService;

    List<User> userList = null;
    Optional<User> options;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        user = new User();
        user.setUserAddedDate(new Date());
        user.setUserId("John123");
        user.setUserMobile("1234567789");
        user.setUserName("john");
        user.setUserPassword("johnpass");
        userList = new ArrayList<>();
        userList.add(user);

        options = Optional.of(user);

    }

这是 User 类,声明为 public @文档 公共类用户{

/*
 * This class should have five fields (userId,userName,
 * userPassword,userMobile,userAddedDate). Out of these five fields, the field
 * userId should be annotated with @Id (This annotation explicitly specifies the document
 * identifier). This class should also contain the getters and setters for the
 * fields, along with the no-arg , parameterized constructor and toString
 * method.The value of userAddedDate should not be accepted from the user but
 * should be always initialized with the system date.
 */

@Id
private String userId;
private String userName;
private String userPassword;
private String userMobile;
private Date userAddedDate;

public User() {
}

public User(String userId, String userName, String userPassword, String userMobile, Date userAddedDate) {
    this.userId = userId;
    this.userName = userName;
    this.userPassword = userPassword;
    this.userMobile = userMobile;
    this.userAddedDate = userAddedDate;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getUserPassword() {
    return userPassword;
}

public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
}

public String getUserMobile() {
    return userMobile;
}

public void setUserMobile(String userMobile) {
    this.userMobile = userMobile;
}

public void setUserAddedDate(Date userAddedDate) {
    this.userAddedDate = userAddedDate;
}

【问题讨论】:

  • 首先你在混合东西。 mockito-core 是您需要的。 mockito-all 是错误的。为什么要添加asm?此外,最好不要添加图像,将文本作为文本放在帖子中......(另见github.com/mockito/mockito/wiki/Declaring-mockito-dependency
  • 同时删除 mockito-all 和 asm。仍然出现这样的错误:
  • org.mockito.exceptions.base.MockitoException: 无法发布模拟 这不应该发生,除非您使用第三方模拟制造商
  • 请显示您正在使用模拟的班级以及您尝试模拟的班级...
  • Mockito 无法模拟此类:接口 com.stackroute.keepnote.repository.UserRepository。 @Repository 公共接口 UserRepository 扩展 MongoRepository { }

标签: spring spring-boot maven mockito


【解决方案1】:

如果您使用的是 Spring boot,则需要删除所有其他依赖项(mockito、asm、jaxb-api)。此入门者spring-boot-starter-test 确保您的测试所需的一切都已经存在并且具有正确的版本。你可以查看pomhere

【讨论】:

  • 之前我只尝试使用这个,但我认为错误是因为 mockito。它也不是那样工作的
  • 确保您的 User 类不是最终的,也不是私有的(不确定什么是私有类可能是私有内部类),否则 Mockito 无法创建代理来模拟您的类
  • 它是公开的,我也在我的问题中添加了这一点。
【解决方案2】:

根据您的“无法发布模拟”评论,我猜您有一个旧版本的 spring-boot-starter-test 依赖项。如果您无法更新 Spring Boot 版本,您可以在 &lt;dependencies&gt;&lt;dependencyManagement&gt; 中更新 mockito 和 byte-buddy:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.6.28</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy-agent</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

当我升级到 Java SE 15 时,我开始看到“无法发布模拟”错误:

org.mockito.exceptions.base.MockitoException: 
Failed to release mocks

This should not happen unless you are using a third-part mock maker

在堆栈跟踪的底部,这是一个过时的字节伙伴问题的线索:

Caused by: java.lang.IllegalArgumentException: Unknown Java version: 15
    at net.bytebuddy.ClassFileVersion.ofJavaVersion(ClassFileVersion.java:232)
    at net.bytebuddy.ClassFileVersion$VersionLocator$ForJava9CapableVm.locate(ClassFileVersion.java:485)

我正在使用的 spring-boot-starter-test 版本具有过时的 mockito 和 byte-buddy 依赖项,它们不知道最新的 Java 版本。更新依赖项应该可以解决您的问题。

【讨论】:

  • 这在 JpaDataTest 中对我不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 2018-02-01
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多