【发布时间】: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