我是测试用例新手,正在尝试使用 JUnit 5 和 Spring Boot 2 学习 mokito 2。 我正在得到
No tests found in StudentServiceTest
Is the method annotated with @Test?
Is the method public?
我用谷歌搜索了很多。但找不到任何可行的解决方案。
构建.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
group 'com.demo.mockito'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.junit.jupiter:junit-jupiter-api:5.2.0')
testCompile('org.junit.jupiter:junit-jupiter-params:5.2.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile 'org.mockito:mockito-core:2.21.0'
testCompile 'org.mockito:mockito-junit-jupiter:2.23.0'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
test {
useJUnitPlatform()
}
}
测试用例
import com.demo.mockito.entity.StudentEntity;
import com.demo.mockito.repo.StudentRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import org.mockito.Mock;
import org.mockito.Mock;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
@DisplayName("Spring boot 2 mockito2 Junit5 example")
public class StudentServiceTest {
@InjectMocks // it will mock and inject all the required dependency
private StudentService studentService;
@Mock // mock because we don't want to call actual repo class
private StudentRepository studentRepository;
public StudentServiceTest() {
}
@BeforeEach
void setMockOutput() {
// because we are mocking repo, we need to define how repo will work in case of call
List<StudentEntity> collect = Stream.of(
new StudentEntity("shrikant", new Date()), new StudentEntity("sharma", new Date())).collect(Collectors.toList());
when(studentRepository.findAll()).thenReturn(collect);
}
@Test
public void findAll() {
assertEquals(2, studentService.findAll().size());
}
存储库
import com.demo.mockito.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface StudentRepository extends JpaRepository<StudentEntity, Long> {
}
服务
package com.demo.mockito.service;
import com.demo.mockito.entity.StudentEntity;
import com.demo.mockito.model.Student;
import com.demo.mockito.repo.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
@Service
public class StudentService {
Logger logger = Logger.getLogger(this.getClass().getName());
@Autowired
private StudentRepository studentRepository;
public List<Student> findAll() {
List<Student> students = new ArrayList<>();
List<StudentEntity> all = studentRepository.findAll();
all.forEach(studentEntity -> students.add(new Student(studentEntity.getRollNo(), studentEntity.getName(), studentEntity.getDate())));
logger.info("StudentService.findAll " + students);
return students;
}
}
我做错了什么? 为了使其工作,我从许多不同的教程中复制了许多额外的代码。因此,如果某些内容是多余的或不是正确的方法,请告诉我。
最佳答案
你的build.gradle
可能需要重新排列一下
- 请移步
test {
useJUnitPlatform()
}
脱离了对 root 的依赖。
您对
'org.springframework.boot:spring-boot-starter-test'
有两次依赖。请删除不排除的一项请删除对
mockito-core
的依赖,因为它是传递性的请删除依赖项
org.mockito:mockito-junit-jupiter
的版本
您可以采用依赖项,以便测试依赖项放在最后
您可以统一使用
实现
和编译
最后它应该类似于:
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
group 'com.demo.mockito'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
testImplementation('org.junit.jupiter:junit-jupiter-api:5.2.0')
testCompile('org.junit.jupiter:junit-jupiter-params:5.2.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0')
testCompile 'org.mockito:mockito-junit-jupiter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Junit4 从类路径中消失后,您将必须更改代码的某些部分(例如 @Runwith
)
此外,我在您的存储库中没有看到 @Repository
注释。
关于java - 使用 Junit 5 和 Spring Boot 2 在 Mokito 2 中未发现测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61519070/