gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:07:53 25 4
gpt4 key购买 nike

我知道 JUnit 5 在底层引入了重大更改,但这不是应该自动配置的吗?

构建.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()
}

存储库,位于 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> {}

测试,位于 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());
}
}

结果:此处发帖时间太长,请参阅here

最佳答案

github 上对这个问题进行了长时间的讨论( https://github.com/spring-projects/spring-framework/issues/22286 )。您有三个选择:

  • 在构造函数中添加注释@Autowired。默认情况下,Spring 不会在测试类的构造函数中进行依赖注入(inject)。不得不提一下。
    @Autowired
public ClassifiedRepositoryTest(TestEntityManager entityManager, ClassifiedRepository classifiedRepository) {
this.entityManager = entityManager;
this.classifiedRepository = classifiedRepository;
}
  • 如 github 上所述:在测试类顶部添加 @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
    @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
@DataJpaTest
public class ClassifiedRepositoryTest { ... }
  • 删除构造函数并使用 @Autowired 注解字段。
@Autowired
private TestEntityManager entityManager;
@Autowired
private ClassifiedRepository classifiedRepository;

关于java - 为什么使用 JUnit 5 运行此 Spring Data JPA Repository 测试会导致 ParameterResolutionException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59657899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com