【问题标题】:No tests found in Mokito 2 with Junit 5 and spring boot 2使用 Junit 5 和 spring boot 2 在 Mokito 2 中未找到测试
【发布时间】:2020-08-14 13:27:45
【问题描述】:

我是测试用例的新手,我正在尝试使用 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;
    }
}

我做错了什么? 为了让它工作,我从很多不同的教程中复制了很多额外的代码。所以请让我知道是否有多余的东西或不正确的做法。

【问题讨论】:

    标签: java spring-boot junit mockito integration-testing


    【解决方案1】:

    您的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 注释。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2020-07-27
        • 2018-08-07
        • 1970-01-01
        • 1970-01-01
        • 2019-05-26
        • 1970-01-01
        • 2019-02-19
        • 2020-11-01
        • 2019-03-25
        相关资源
        最近更新 更多