简体   繁体   English

如何为实现 ApplicationListener 的 class 编写 JUNIT 测试用例<applicationpreparedevent></applicationpreparedevent>

[英]How to write JUNIT test case for a class which implements ApplicationListener<ApplicationPreparedEvent>

This is my class in a Spring boot application.这是我在 Spring 启动应用程序中的 class。 I am trying to write JUNIT test case for the method onApplicationEvent我正在尝试为 onApplicationEvent 方法编写 JUNIT 测试用例

public class MyUtil implements ApplicationListener<ApplicationPreparedEvent>
{
    @Override
    public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent)
    {
        ConfigurableEnvironment configurableEnvironment = applicationPreparedEvent.getApplicationContext()
                .getEnvironment();
        String vaultSecretId = configurableEnvironment.getProperty("vault.secret.id");
        String vaultRoleId = configurableEnvironment.getProperty("vault.role.id");

        //Make REST call and fetch key value pair and set vaule in property.

        for (Map.Entry<String, String> entry : allSecrets.entrySet())
        {
            System.setProperty(entry.getKey(), entry.getValue());
        }
    }
}

My JUNIT Test case我的 JUNIT 测试用例

When I run the below test case.当我运行以下测试用例时。 I am getting null for configurableEnvironment in MyUtil class我得到 null 用于 MyUtil class 中的可配置环境

@Test
public void testSuccess()
{
    MyUtil myUtil = new MyUtil();
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("vault.secret.id", "somesecretid");
    mockEnvironment.setProperty("vault.role.id", "someroleid");
    SpringApplication application = new SpringApplication();
    String[] args = new String[1];
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    context.setEnvironment(mockEnvironment);
    ApplicationPreparedEvent event = new ApplicationPreparedEvent(application, args, context);
    // event.getApplicationContext().setEnvironment(mockEnvironment);  - Tried this but no luck
    myUtil.onApplicationEvent(event);
}

I would suggest leveraging Spring's test features, no mocks needed.我建议利用 Spring 的测试功能,不需要模拟。

Note: This example uses Junit Jupiter注意:本例使用 Junit Jupiter


import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig
public class Q62049482 {

    @Autowired
    ConfigurableApplicationContext configurableApplicationContext;


    @Test
    public void testSuccess() {
        var ape = new ApplicationPreparedEvent(new SpringApplication(), null, this.configurableApplicationContext);
        var myUtil = new MyUtil();
        myUtil.onApplicationEvent(ape);
        // add assertions here
    }

    public static class MyUtil implements ApplicationListener<ApplicationPreparedEvent> {

        @Override
        public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent) {

            ConfigurableEnvironment configurableEnvironment = applicationPreparedEvent.getApplicationContext()
                    .getEnvironment();

            String vaultSecretId = configurableEnvironment.getProperty("vault.secret.id");

            String vaultRoleId = configurableEnvironment.getProperty("vault.role.id");

            //
            // //Make REST call and fetch key value pair and set vaule in property.
            //
            // for (Map.Entry<String, String> entry : allSecrets.entrySet())
            // {
            // System.setProperty(entry.getKey(), entry.getValue());
            // }

        }
    }
}

I was able to make it work by adding StaticApplicationContext.我能够通过添加 StaticApplicationContext 使其工作。 There might be a better way.可能有更好的方法。 Please do let me know, if there is a better way of writing test case for this scenario.如果有更好的方法为这种情况编写测试用例,请告诉我。

@Test
public void testSuccess()
{
    MockEnvironment mockEnvironment = new MockEnvironment();
    mockEnvironment.setProperty("vault.secret.id", "somesecretid");
    mockEnvironment.setProperty("vault.role.id", "someroleid");
    SpringApplication application = new SpringApplication();
    String[] args = new String[1];
    StaticApplicationContext staticApplicationContext = new StaticApplicationContext();
    staticApplicationContext.setEnvironment(mockEnvironment);
    ApplicationPreparedEvent event = new ApplicationPreparedEvent(application, args, staticApplicationContext);
    MyUtil myUtil = new MyUtil();
    myUtil.onApplicationEvent(event);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM