在配备Junit 5和弹簧靴2的Mokito 2中未发现任何测试



我是测试用例的新手,并尝试使用JUnit 5和spring boot 2学习mokito 2。我得到

No tests found in StudentServiceTest
Is the method annotated with @Test?
Is the method public?

我在谷歌上搜索了很多。但找不到任何有效的解决方案。

build.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可能有点重排

  1. 请移动
test {
useJUnitPlatform()
}

从依赖项到根。

  1. 您对'org.springframework.boot:spring-boot-starter-test'有两次依赖。请删除不排除的

  2. 请删除对mockito-core的依赖,因为它是在过渡中出现的

  3. 请删除依赖项org.mockito:mockito-junit-jupiter的版本

  4. 您可以求助于依赖项,这样测试依赖项就可以最后

  5. 可以统一使用implementationcompile

最后它应该看起来类似于:

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());
}
}

build.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'
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 org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<StudentEntity, Long> {
}

相关内容

最新更新