gpt4 book ai didi

java - Junit 测试不会取代 placeHolders

转载 作者:行者123 更新时间:2023-12-01 20:00:09 25 4
gpt4 key购买 nike

我在 Maven 项目中有这个配置类:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import lombok.Data;

@Data
@Configuration
public class SmsConfig {

@Value("${sms.domainId}")
private String domainId;

@Value("${sms.gateway.url}")
private String gatewayUrl;

@Value("${sms.cmd}")
private String cmd;

@Value("${sms.login}")
private String login;

@Value("${sms.passwd}")
private String passwd;

}

我在 Spring 项目中有这个服务类:

Service("smsService")
public class AltiriaSMSRestServiceImpl implements SmsService {

private final SmsConfig smsConfig;

public AltiriaSMSRestServiceImpl(SmsConfig smsConfig) {
this.smsConfig = smsConfig;
}

@Override
public boolean sendSMS(String msg, String to) throws Exception {
...
}
...
}

这个测试:

@ContextConfiguration(classes = { SmsConfig.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class AltiriaSMSRestServiceImplTest {

@Autowired
@Qualifier("smsService")
private AltiriaSMSRestServiceImpl smsService;

@Test
public void testSendSMS() throws Exception {
smsService.sendSMS("this is a test", "+34776498");
}

}

在 IntelliJ IDEA 上,配置类上的值似乎设置正确

enter image description here

但是当我运行测试时,Junit 测试不会替换 placeHolders

5:25:40.009 [main] DEBUG org.springframework.web.client.RestTemplate - Writing [{ "credentials":{"domainId":"${sms.domainId}","login":"${sms.login}","passwd":"${sms.passwd}"},"destination":["32470855126"], "message":{"msg":"this is a test","concat":"true", "encoding":"unicode"} }] as "application/json"

这是我的 pom.xml:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>

<dependencies>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>


<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<artifactId>lombok</artifactId>
<groupId>org.projectlombok</groupId>
<scope>provided</scope>
<version>1.18.10</version>
</dependency>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.14.0</version>
<scope>test</scope>
</dependency>



</dependencies>

最佳答案

@ContextConfiguration(classes = { SmsConfig.class, AltiriaSMSRestServiceImpl.class})
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations="classpath:application.properties")
public class AltiriaSMSRestServiceImplTest {
..
}

关于java - Junit 测试不会取代 placeHolders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008681/

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