How to Mock External Services and Encrypt Configs in SpringBoot
This article demonstrates practical SpringBoot techniques for masking external dependencies by mocking beans directly in the container and securing configuration values with Jasypt encryption, enabling local debugging without full service startup and protecting sensitive credentials.
Masking External Dependencies
When a SpringBoot application depends on external services (e.g., OrderServiceClient in a SpringCloud project), starting the whole distributed system locally can be heavy. The article proposes mocking those external beans directly in the Spring container so the application can run without starting other services.
Steps to Mock a Bean
After SpringBoot starts, locate the bean that represents the external API.
Remove the bean definition from the container.
Create a mock instance (using PowerMockito) that returns a predefined response.
Register the mock as a singleton bean.
@Override
public BaseResponse<OrderNoResVO> getUserByHystrix(@RequestBody UserReqVO userReqVO) {
OrderNoReqVO vo = new OrderNoReqVO();
vo.setAppId(123L);
vo.setReqNo(userReqVO.getReqNo());
BaseResponse<OrderNoResVO> orderNo = orderServiceClient.getOrderNo(vo);
return orderNo;
}Running the application shows that the original OrderServiceClient is replaced by the mock, avoiding remote calls.
Configuring Encryption
To avoid storing plain‑text credentials, the article introduces the Jasypt Spring Boot starter. Adding a single Maven dependency enables encryption of configuration values and automatic decryption at runtime.
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>A test can generate encrypted strings:
@Autowired
StringEncryptor encryptor;
@Test
public void getPass() {
String name = encryptor.encrypt("userName");
String password = encryptor.encrypt("password");
System.out.println(name + "----------------");
System.out.println(password + "----------------");
}For datasource properties, a BeanPostProcessor decrypts the username and password before the DataSource is created.
@Component
public class DataSourceProcess implements BeanPostProcessor {
@Autowired
private StringEncryptor encryptor;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof DataSourceProperties) {
DataSourceProperties ds = (DataSourceProperties) bean;
ds.setUsername(encryptor.decrypt(ds.getUsername()));
ds.setPassword(encryptor.decrypt(ds.getPassword()));
return ds;
}
return bean;
}
}The encrypted values can be supplied via command‑line arguments, e.g., -Djasypt.encryptor.password=password.
Conclusion
The two techniques—mocking external beans and encrypting configuration—allow developers to run SpringBoot services locally with minimal dependencies and keep sensitive data safe.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
