在带有 Junit 5 和 spring boot 2 的 Mokito 2 中找不到测试

No tests found in Mokito 2 with Junit 5 and spring boot 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()
}

不依赖于 root。

  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> {
}