gpt4 book ai didi

java - SpringBoot : Running a Junit unit test with minimal configuration

转载 作者:行者123 更新时间:2023-12-01 18:33:36 26 4
gpt4 key购买 nike

我有一个 Springboot 项目,在该项目中,我找到了一种创建和运行简单的 Junit 测试用例的方法,该测试用例查看存储库并获取给定实体的一些数据属性。 Junit 运行的结果是通过,因此没有问题。

但问题是,我看到了很多示例,其中教程展示了 Springboot 项目,在这些项目中,他们可以仅使用 运行 Junit 测试>@Runwith@SpringBootTest针对他们的特定测试类(class)。

就我而言,我必须添加 3 个注释,@SpringBootTest@RunWith 以及 @ContextConfiguation(带参数) 直到我能够运行测试用例。

所以我的问题是我如何才能尽可能简单地运行它,(我见过的一些练习的 springboot 测试类只有一个注释)

我的 Springboot 测试类如下所示:

Screenshot of my Junit class

我的目录结构如下所示:

Screenshot of my Project directory structure

我的application.properties看起来像这样:

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/erfan
spring.datasource.username=erfan
spring.datasource.password=

#Some additional properties is trying to be set by Spring framework so this must be set
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true


#spring.datasource.initialization-mode=always
#spring.datasource.initialize=true
#spring.datasource.schema=classpath:/schema.sql
#spring.datasource.continue-on-error=true

#HikariCP is a ConnectionPool manager, related to DB stuff



#Below is the property key you need to set to * as value to expose all kind of monitoring related information
#about your web application

#management.endpoints.web.exposure.include=*

还有我的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<groupId>com.sample</groupId>
<artifactId>postgres</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>postgres</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>



</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>



</plugins>






</build>
</project>

那么我是否缺少 application.properties 文件中的某些内容?我应该包含一些内容才能删除测试类中的“样板”注释?

最佳答案

取决于您想要做什么。基本上,spring 具有自定义注释,可将 spring 上下文配置为仅包含相关 bean。这就是所谓的test slices .

但是我总是尝试遵循一些“规则”:

  • 避免 @SpringBootTest除非您正在进行集成测试,或手动设置要使用的类 @SpringBootTest(classes = {MyService1.class, MyService2.class}
  • 如果您正在测试 spring jpa,则可以使用 @DataJpaTest注释,示例 here
  • 如果您正在测试 Controller ,则可以使用 @WebMvcTest ,例如here
  • 如果您正在测试其他服务,您可以随时使用 @ContextConfiguration相应地配置 spring 上下文。

例如,对于您的测试,我会用以下两种方式之一编写它:

@RunWith(SpringRunner.class)
@DataJpaTest
@Import(AnotherConfig.class)
class MyTest {
// test stuff here
}
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {AnotherConfig.class})
// NOTE, if you have a JpaConfig class where you @EnableJpaRepositories
// you can instead add this config class to the @ContextConfiguration classes
@EnableJpaRepositories
class MyTest {
// test stuff here
}

基本上,不用担心测试上有多少注释,而是担心哪些 bean/服务正在被 Autowiring 。例如@SpringBootTest是单个注释,但会 Autowiring Spring 上下文中的所有 bean。

关于java - SpringBoot : Running a Junit unit test with minimal configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60113060/

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