当 applicationcontext 根据 application.properties 中的环境值初始化时 Springboot 单元测试

发布于 2025-01-11 21:10:05 字数 1689 浏览 0 评论 0原文

我现在有一个工作的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-01-18 21:10:05

附带说明:如果注入每个依赖项,您的代码将更容易测试,例如:

public IDeploymentHandler getDeploymentInterface( @Value("${deployment:}") String deploymentEnv, ApplicationContext ctx ) throws InvalidConfigException {

一般来说,从上下文中获取 bean 并不是一个好的策略;这与 Spring 的理念相反:告诉你想要什么,但不要索取。

As a side note: You make your code easier to test if you inject every dependency, like:

public IDeploymentHandler getDeploymentInterface( @Value("${deployment:}") String deploymentEnv, ApplicationContext ctx ) throws InvalidConfigException {

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文