简体   繁体   中英

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

This is my class in a Spring boot application. I am trying to write JUNIT test case for the method onApplicationEvent

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

When I run the below test case. I am getting null for configurableEnvironment in 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.

Note: This example uses 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. 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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