Java 在使用JUnit5和SpringBoot2的Mokito2中未找到任何测试
我是测试用例的新手,并尝试使用JUnit5和SpringBoot2学习Mokito2。 我越来越Java 在使用JUnit5和SpringBoot2的Mokito2中未找到任何测试,java,spring-boot,junit,mockito,integration-testing,Java,Spring Boot,Junit,Mockito,Integration Testing,我是测试用例的新手,并尝试使用JUnit5和SpringBoot2学习Mokito2。 我越来越 No tests found in StudentServiceTest Is the method annotated with @Test? Is the method public? 我在谷歌上搜索了很多。但找不到任何有效的解决办法 格雷德尔先生 plugins { id 'java' id 'org.springframework.boot' version '2.
No tests found in StudentServiceTest
Is the method annotated with @Test?
Is the method public?
我在谷歌上搜索了很多。但找不到任何有效的解决办法
格雷德尔先生
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()
}
}
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()
}
测试用例
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;
导入com.demo.mockito.repo.StudentRepository;
导入org.junit.jupiter.api.beforeach;
导入org.junit.jupiter.api.DisplayName;
导入org.junit.jupiter.api.Test;
导入org.junit.jupiter.api.extension.extensedWith;
导入org.junit.runner.RunWith;
导入org.mockito.InjectMocks;
导入org.mockito.junit.MockitoJUnitRunner;
导入org.mockito.junit.jupiter.MockitoExtension;
导入org.springframework.beans.factory.annotation.Autowired;
导入java.util.Date;
导入java.util.List;
导入java.util.stream.collector;
导入java.util.stream.stream;
导入静态org.junit.Assert.assertEquals;
导入静态org.junit.Assert.assertThat;
导入静态org.mockito.mockito.when;
导入org.mockito.Mock;
导入org.mockito.Mock;
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
@DisplayName(“SpringBoot2Mockito2JUnit5示例”)
公共班级学生服务测试{
@InjectMocks//它将模拟并注入所有必需的依赖项
私人学生服务学生服务;
@Mock//Mock,因为我们不想调用实际的repo类
私立学校;私立学校;
公共学生服务测试(){
}
@之前
void setMockOutput(){
//因为我们在模仿回购协议,所以我们需要定义回购协议在调用时的工作方式
List collect=Stream.of(
新学生实体(“shrikant”,new Date())、新学生实体(“sharma”,new Date()).collect(Collectors.toList());
当(studentRepository.findAll())。然后返回(collect);
}
@试验
公共无效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> {
}
import com.demo.mockito.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<StudentEntity, Long> {
}
import com.demo.mockito.entity.StudentEntity;
导入org.springframework.data.jpa.repository.JpaRepository;
公共界面StudentRepository扩展了JpaRepository{
}
服务
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;
}
}
package com.demo.mockito.service;
导入com.demo.mockito.entity.StudentEntity;
导入com.demo.mockito.model.Student;
导入com.demo.mockito.repo.StudentRepository;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Optional;
导入java.util.logging.Logger;
@服务
公共班级学生服务{
Logger Logger=Logger.getLogger(this.getClass().getName());
@自动连线
私立学校;私立学校;
公共列表findAll(){
List students=new ArrayList();
List all=studentRepository.findAll();
all.forEach(studentEntity->students.add(newstudent)(studentEntity.getRollNo(),studentEntity.getName(),studentEntity.getDate());
logger.info(“StudentService.findAll”+学生);
留学生;
}
}
我做错了什么?
为了让它发挥作用,我从许多不同的教程中复制了许多额外的代码。因此,请告诉我是否有多余的内容或不正确的方法。您的
构建。gradle
可能需要重新安排
'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
注释。谢谢Micheal,只是为了帮助其他遇到同样问题的人
import com.demo.mockito.entity.StudentEntity;
import com.demo.mockito.repo.StudentRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@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() {
}
@Test
public void findAll() {
// 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);
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> {
}
import com.demo.mockito.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<StudentEntity, Long> {
}
import com.demo.mockito.entity.StudentEntity;
导入org.springframework.data.jpa.repository.JpaRepository;
导入org.springframework.stereotype.Repository;
@存储库
公共界面StudentRepository扩展了JpaRepository{
}