简体   繁体   English

为什么使用 JUnit 5 运行此 Spring Data JPA Repository 测试会导致 ParameterResolutionException?

[英]Why does running this Spring Data JPA Repository test with JUnit 5 cause a ParameterResolutionException?

I understand that JUnit 5 introduced substantial changes under the hood, however isnt't this supposed to be configured automatically?我知道 JUnit 5 在幕后引入了实质性的变化,但是这不是应该自动配置的吗?

build.gradle:构建.gradle:

plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
    mavenCentral()
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.imgscalr:imgscalr-lib:4.2'
    runtimeOnly 'com.h2database:h2'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
test {
    useJUnitPlatform()
}

Repository, located in src/main/java/com/example/repositories:存储库,位于 src/main/java/com/example/repositories:

import org.adventure.entities.Classified;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ClassifiedRepository extends CrudRepository<Classified, Long> {}

Test, located in src/test/java/com/example/repositories:测试,位于 src/test/java/com/example/repositories:

import org.adventure.entities.Classified;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertTrue;

@DataJpaTest
public class ClassifiedRepositoryTest {

    private TestEntityManager entityManager;
    private ClassifiedRepository classifiedRepository;

    public ClassifiedRepositoryTest(TestEntityManager entityManager, ClassifiedRepository classifiedRepository) {
        this.entityManager = entityManager;
        this.classifiedRepository = classifiedRepository;
    }

    @Test
    public void classifiedCanBeFoundById() {
        // GIVEN
        Classified classified = new Classified("Cheap bike",
                "Worn bike, used on a hobby level. Needs oiling", 300, "EUR");
        entityManager.persist(classified);
        entityManager.flush();

        // WHEN
        Optional<Classified> found = classifiedRepository.findById(classified.getId());

        // THEN
        assertTrue(found.isPresent());
    }
}

Result: Too long to post here, please see here结果:太长无法在此处发布,请参阅此处

There was a long discussion about this problem on github ( https://github.com/spring-projects/spring-framework/issues/22286 ).在 github ( https://github.com/spring-projects/spring-framework/issues/22286 ) 上对此问题进行了长时间的讨论。 You have three options:您有三个选择:

  • Add the annotation @Autowired on your constructor.在构造函数上添加注释@Autowired By default, Spring does not make dependency injection in a contructor of a test class.默认情况下,Spring 不会在测试类的构造函数中进行依赖注入。 You have to mention it.你必须提到它。
    @Autowired
    public ClassifiedRepositoryTest(TestEntityManager entityManager, ClassifiedRepository classifiedRepository) {
        this.entityManager = entityManager;
        this.classifiedRepository = classifiedRepository;
    }
  • As discussed on github: Add @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) at the top of your test class.正如在 github 上讨论的:在测试类的顶部添加@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
    @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
    @DataJpaTest
    public class ClassifiedRepositoryTest { ... }
  • Remove your constructor and annotate your fields with @Autowired .删除您的构造函数并使用@Autowired注释您的字段。
@Autowired
private TestEntityManager entityManager;
@Autowired
private ClassifiedRepository classifiedRepository;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的Spring Boot自动连接的JPA存储库未通过JUnit测试? - Why is my Spring Boot autowired JPA Repository failing JUnit test? Spring数据仓库JUnit测试 - Spring data repository junit Test Spring和JPA:运行JUnit测试时出错 - Spring and JPA: error while running JUnit test Spring Boot应用程序中的JUnit测试中没有自动装配JPA存储库的合格Bean - No qualifying bean for autowired JPA repository in JUnit test in Spring Boot application 为什么我的Spring JUnit测试规则未运行? - Why is my Spring JUnit Test Rule not running? 从Spring Data JPA 1.4.x升级到更新的版本(例如1.7.1)后,为什么我的中间存储库接口会引起麻烦? - Why does my intermediate repository interface cause trouble after upgrading from Spring Data JPA 1.4.x to a more recent version (e.g. 1.7.1)? spring jpa存储库在哪里存储数据? - where does spring jpa repository store data? 存储库的 Spring JPA 数据扫描不起作用 - Spring JPA Data scanning for repository does not work repository.save() 是如何工作的,以及如何测试 Spring 数据中的唯一约束 JPA - How does repository.save() work, and how to test unique constrains in Spring data JPA Soap Web 服务的 JUnit 5 测试 - ParameterResolutionException:未注册 ParameterResolver - JUnit 5 test for Soap Web Service - ParameterResolutionException: No ParameterResolver registered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM