gpt4 book ai didi

spring-boot - Spring @DataJpaTest 与 JUnit 5

转载 作者:行者123 更新时间:2023-12-01 15:32:00 26 4
gpt4 key购买 nike

我似乎无法在网上找到一种特定的标准方法来制作 @DataJpaTest正确运行。
@DataJpaTest 是真的吗?现在没有使用,所有测试都使用 @SpringBootTest 在服务或 Controller 级别运行?

    @Repository
public interface MyBeanRepository extends JpaRepository<MyBean, Long> {
}

@Configuration
@EnableJpaRepositories("com.app.repository.*")
@ComponentScan(basePackages = { "com.app.repository.*" })
public class ConfigurationRepository {
}


@Entity
public class MyBean {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

@Version
@Column(name = "version")
private Integer version;

@NotNull
@Size(min = 2)
private String name;
}

@DataJpaTest
public class MyBeanIntegrationTest {

@Autowired
MyBeanRepository myBeanRepository;

@Test
public void testMarkerMethod() {
}

@Test
public void testCount() {
Assertions.assertNotNull(myBeanRepository , "Data on demand for 'MyBean' failed to initialize correctly");
}
}

使用 Eclipse->Run Junit 运行会显示这些日志。
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
at org.springframework.util.Assert.state(Assert.java:73)

使用 gradle test 运行会显示 init 失败的错误。
FAILURE: Build failed with an exception.

* What went wrong:
Test failed.
Failed tests:
Test com.app.repository.MyBeanIntegrationTest#initializationError (Task: :test)

这是gradle脚本。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin")
}
}

plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
id 'eclipse'

}

apply plugin: 'io.spring.dependency-management'

group = 'com.app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenLocal()
jcenter()
mavenCentral()
}

test {
useJUnitPlatform()
}

dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'org.hsqldb:hsqldb'
testImplementation ('org.springframework.boot:spring-boot-starter-test') {
// exlcuding junit 4
exclude group: 'junit', module: 'junit'
}

/**
Test Dependencies Follows
**/


// junit 5 api
testCompile "org.junit.jupiter:junit-jupiter-api:5.2.0"
// For junit5 parameterised test support
testCompile "org.junit.jupiter:junit-jupiter-params:5.2.0"
// junit 5 implementation
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.2.0"
// Only required to run junit5 test from IDE
testRuntime "org.junit.platform:junit-platform-launcher"

}

编辑:

这已在同一个存储库中解决并提交。
https://github.com/john77eipe/spring-demo-1-test

最佳答案

添加 Github 链接是个好主意。我可以在那里看到以下问题:

1) 如果你不能有一个用 @SpringBootApplication 注释的主类, 您可以使用:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.app.repository")
public class MySampleApplication {
}

2) 更改您的 ConfigurationRepository 上的注释上课:

@EnableJpaRepositories("com.app.repository")
@ComponentScan(basePackages = { "com.app.repository" })
public class ConfigurationRepository {

那应该让我们继续下一点:

3) 您的 MyBeanIntegrationTest应注释为:

@SpringBootTest(classes = MyAppApplication.class)
public class MyBeanIntegrationTest {

4) 在 application.yml最后一行的缩进有一个小问题。将制表符转换为空格,应该没问题。

5) 接下来是 MyBeanRepository界面。您不能使用名为 findOne 的方法那里。问题是,在标记为 JpaRepository 的接口(interface)中或 CrudRepository等等,方法名称需要遵循一定的规则。如果您标记它将是一个包含类型 MyBean 的存储库您的方法名称应更改为 findById ,因为 Spring 将查找名为 id 的属性在你的 bean 里。将其命名为 findOne将导致测试失败:
No property findOne found for type MyBean!

修复这些东西后,您的测试通过了我的环境。

我希望这有帮助!

关于spring-boot - Spring @DataJpaTest 与 JUnit 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56214710/

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