【发布时间】:2019-10-08 17:31:53
【问题描述】:
我正在开发一个 Spring Boot 项目,它的控制器、服务、存储库运行良好。我用 POSTMAN 对它们进行了测试。
我是 mockito 新手,正在尝试测试我的项目。 我有以下测试类:
package com.spr;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import com.spr.entity.User;
import com.spr.repository.UserRepository;
import com.spr.service.UserService;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
@Autowired
private UserService userService;
@MockBean
private UserRepository userRepository;
@Before
public void init() {
}
@Test
public void getAllUsersTest() {
List<User> users = new ArrayList<User>();
users.add(new User("User1", "user1@email.com", "male"));
users.add(new User("User2", "user2@email.com", "female"));
when(userRepository.findAll()).thenReturn(users);
List<User> userList = userService.getUsers();
assertEquals(2, userList.size());
verify(userRepository, times(1)).findAll();
}
}
我正在使用 Spring Source ToolSuite 3.9.9
我右键单击项目 -> 运行方式 -> jUnit Test
我收到此错误:
Caused by: org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: interface com.spr.repository.UserRepository.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.5-b02
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_05-b13
JVM info : mixed mode
OS name : Windows 8.1
OS version : 6.3
我在这里做错了什么?
编辑:
这是我的 pom.xml 和 UserRepository
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.spr</groupId>
<artifactId>testboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SprBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- spring mvc, rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- unit test rest -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mysql dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- test patch operation need this -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
用户存储库
package com.spr.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.spr.entity.User;
public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User>{
}
我在类路径中有 mockito-core-2.23.4.jar
【问题讨论】:
-
你的mockito版本是什么?还有弹簧靴?
-
请添加
UserRepository。 -
您是否尝试过添加新的
Mockito版本? -
是的,我做了,但同样的错误:(我也尝试了其他版本
标签: spring spring-boot mockito spring-boot-test