当 applicationcontext 根据 application.properties 中的环境值初始化时 Springboot 单元测试
我现在有一个工作的 Spring Boot 应用程序并添加单元测试。我正在尝试为服务层编写一个单元测试,该层有两个可能的处理程序。在某一时刻,任何一个处理程序都将根据服务的部署而处于活动状态。
我的代码如下所示。
配置-
package com.org.app.configs;
@Configuration
public class DeploymentFactoryManager {
@Value("${deployment:}")
private String deploymentEnv;
@Autowired
ApplicationContext ctx;
@Bean
public IDeploymentHandler getDeploymentInterface() throws InvalidConfigException {
IDeploymentHandler deploymentHandler = null;
if(deploymentEnv.equalsIgnoreCase("kubernetes")){
deploymentHandler = ctx.getBean(KubernetesDeployHandler.class);
}else if(deploymentEnv.equalsIgnoreCase("docker")){
deploymentHandler = ctx.getBean(DockerDeployHandler.class);
} else {
throw new InvalidConfigException("Unknown queue type : "+deploymentEnv);
}
return deploymentHandler;
}
}
服务-
package com.org.app.services;
public List<String> status() throws InvalidConfigException{
@Autowired
DeploymentFactoryManager deploymentFactoryManager;
public List<String> getStatus(){
IDeploymentHandler iDeploymentHandler = deploymentFactoryManager.getDeploymentInterface();
return iDeploymentHandler.list();
}
我在编写单元测试时遇到问题。到目前为止,我已经尝试使用 ReflectionTestUtils
设置字段,但失败并显示消息 必须指定字段的目标对象或目标类。
我尝试使用 @TestPropertySource
但这似乎也不起作用。代码似乎不适合单元测试。
@BeforeAll
public static void setup(){
ReflectionTestUtils.setField(deploymentFactoryManager, "deploymentEnv", "kubernetes");
}
有什么建议吗?
I have a working spring boot application and adding unit tests now. I am trying to write a unit test for the Service layer which has two possible handlers. At a time any one of the handlers will be active based on deployment of the service.
My code looks like below.
Config-
package com.org.app.configs;
@Configuration
public class DeploymentFactoryManager {
@Value("${deployment:}")
private String deploymentEnv;
@Autowired
ApplicationContext ctx;
@Bean
public IDeploymentHandler getDeploymentInterface() throws InvalidConfigException {
IDeploymentHandler deploymentHandler = null;
if(deploymentEnv.equalsIgnoreCase("kubernetes")){
deploymentHandler = ctx.getBean(KubernetesDeployHandler.class);
}else if(deploymentEnv.equalsIgnoreCase("docker")){
deploymentHandler = ctx.getBean(DockerDeployHandler.class);
} else {
throw new InvalidConfigException("Unknown queue type : "+deploymentEnv);
}
return deploymentHandler;
}
}
Service-
package com.org.app.services;
public List<String> status() throws InvalidConfigException{
@Autowired
DeploymentFactoryManager deploymentFactoryManager;
public List<String> getStatus(){
IDeploymentHandler iDeploymentHandler = deploymentFactoryManager.getDeploymentInterface();
return iDeploymentHandler.list();
}
I am facing an issue while writing unit tests. So far I have already tried with ReflectionTestUtils
to set the field but it fails with the message Either target object or target class for the field must be specified.
I tried with @TestPropertySource
but that also doesn't seem to work. Code doesn't seem unit test friendly.
@BeforeAll
public static void setup(){
ReflectionTestUtils.setField(deploymentFactoryManager, "deploymentEnv", "kubernetes");
}
Any suggestion would be appreciated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
附带说明:如果注入每个依赖项,您的代码将更容易测试,例如:
一般来说,从上下文中获取 bean 并不是一个好的策略;这与 Spring 的理念相反:告诉你想要什么,但不要索取。
As a side note: You make your code easier to test if you inject every dependency, like:
In general, it's not a good strategy to get beans out of the context; it's the opposite of the Spring idea: tell what u want and don't ask for it.