如何配置我的Spring Boot应用程序,以便在运行单元测试时,它将使用内存数据库,如H2/HSQL,但是当我运行Spring Boot应用程序时,它将使用生产数据库[Postgre/MySQL]?
spring spring-test spring-data spring-test-dbunit spring-boot
在Spring Test配置上使用@Profile和@ActiveProfiles有什么区别
@Configuration
@EnableRetry
@ActiveProfiles("unittest")
static class ContextConfiguration {
Run Code Online (Sandbox Code Playgroud)
和
@Configuration
@EnableRetry
@Profile("unittest")
static class ContextConfiguration {
Run Code Online (Sandbox Code Playgroud) 我想对Spring-boot进行Junit测试,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationTest.class})
public class TestOnSpring {
@Value("${app.name}")
private String appName;
@Test
public void testValue(){
System.out.println(appName);
}
}
Run Code Online (Sandbox Code Playgroud)
和ApplicationTest.java这样
@ComponentScan("org.nerve.jiepu")
@EnableAutoConfiguration()
public class ApplicationTest {
public static void main(String[] args) {
SpringApplication.run(ApplicationTest.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
我的POM是这样的:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</parent>
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我得到了以下错误信息
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'app.name' in string value "${app.name}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:807)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543)
... 31 more
Run Code Online (Sandbox Code Playgroud)
但是当我将此应用程序作为普通Java应用程序运行时
@SpringBootApplication
public …Run Code Online (Sandbox Code Playgroud) 我在 quarkus application.properties 中配置了一个值
skipvaluecheck=true
Run Code Online (Sandbox Code Playgroud)
现在,每当我想要执行测试时,我都希望将此值设置为 false 而不是 true。但我不想更改application.properties,因为它会影响最新的应用程序部署。我只是希望我的测试以 false 值执行,以便我的测试覆盖范围在声纳中变为绿色。
从java代码中,我通过下面的操作获取这个值
ConfigProvider.getConfig().getValue("skipvaluecheck", Boolean.class);
Run Code Online (Sandbox Code Playgroud)
Sprint boot 中已经存在类似的东西,我很好奇 quarkus 中是否也存在这样的东西
考虑以下示例:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {
"some.property=valueA"
})
public class ServiceTest {
@Test
public void testA() { ... }
@Test
public void testB() { ... }
@Test
public void testC() { ... }
}
Run Code Online (Sandbox Code Playgroud)
我正在使用SpringBootTest注释的properties属性来设置some.property此测试套件中所有测试的属性值。现在,我想为其中一个测试(假设testC)设置此属性的另一个值,而不影响其他测试。我该如何实现?我已经阅读了Spring Boot docs的“测试”一章,但是没有找到与我的用例匹配的内容。
我想覆盖测试中application.properties中定义的属性,但@TestPropertySource只允许提供预定义的值.
我需要的是在随机端口N上启动服务器,然后将此端口传递给spring-boot应用程序.端口必须是短暂的,以允许同时在同一主机上运行多个测试.
我不是指嵌入式http服务器(jetty),而是在测试开始时启动的一些不同的服务器(例如zookeeper)和被测试的应用程序必须连接到它.
实现这一目标的最佳方法是什么?
(这里是一个类似的问题,但答案没有提到临时端口的解决方案 - 在Junit Test中覆盖默认的Spring-Boot application.properties设置)
我想创建集成测试,其中Spring Boot将使用@Value注释从.properties文件中读取值.
但每次我运行测试时,我的断言都会失败,因为Spring无法读取值:
org.junit.ComparisonFailure:
Expected :works!
Actual :${test}
Run Code Online (Sandbox Code Playgroud)
我的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebTests.ConfigurationClass.class, WebTests.ClassToTest.class})
public class WebTests {
@Configuration
@ActiveProfiles("test")
static class ConfigurationClass {}
@Component
static class ClassToTest{
@Value("${test}")
private String test;
}
@Autowired
private ClassToTest config;
@Test
public void testTransferService() {
Assert.assertEquals(config.test, "works!");
}
}
Run Code Online (Sandbox Code Playgroud)
src/main/resource包下的application-test.properties包含:
test=works!
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?我该如何解决?
任何帮助高度赞赏.
各位专家下午好,
我有一个要求,我将调用 3 个 REST API 的顺序调用作为单个客户端调用 GET /offers 的一部分,以检索百货商店不同通道中每种产品的可用报价,如下所示
获取产品 /product/{productId/offers 的所有报价
要@Service使用 RestTemplate 交换方法在我的类中执行此操作:
ResponseEntity aisles=restTemplate.exchange(url, HttpMethod.GET, requestEntity, Aisles.class);
aisleId然后循环检索每个产品并调用第二个 API 来获取产品
ResponseEntity<Products> products= restTemplate.exchange(url,
HttpMethod.GET, requestEntity, Products.class);
Run Code Online (Sandbox Code Playgroud)
productId然后循环检索每个并调用第三个 API 来获取 Offers
最后整理所有回复,将报价列表发送给客户。
现在,我对用于编写 JUnit 的 mockito 框架很陌生。我的服务类有一个名为retrieveAllOffers() 的方法,其中有如上所述的 3 个交换方法。
我想知道如何在 Junit 中模拟这 3 个调用才能获得成功的响应场景。
非常感谢您的帮助。
我不想加载整个Spring Boot配置来对我的DAO图层进行单元测试,因此创建了一个嵌套的配置类来抑制默认配置。但是,当我尝试指定要在测试之前运行的SQL脚本时,它找不到它们。
这是代码:
package com.test.customer.controller;
..
@RunWith(SpringRunner.class)
@JdbcTest
@Sql({"data.sql"})
public class InterviewInformationControllerTest {
@Configuration
static class TestConfiguration{
}
@Test
public void testCustomer() {
// code
}
}
I get the error: Cannot read SQL script from class path resource [com/test/customer/controller/data.sql]; nested exception is java.io.FileNotFoundException: class path resource [com/test/customer/controller/data.sql] cannot be opened because it does not exist
Run Code Online (Sandbox Code Playgroud)
我尝试将文件放置在src/main/resources(不喜欢)和src/test/resources(我喜欢)这两个位置
注意:我正在通过Eclipse在Eclipse中运行单元测试Run as -> JUnit test。
编辑:将static关键字添加到配置类
spring ×7
java ×6
spring-boot ×6
junit ×5
junit4 ×1
mockito ×1
quarkus ×1
spring-data ×1
spring-test ×1