0

Kindly let me know how we can test below piece of code in junit. ConfigProperty is reading the values from application.property file. now i have to write an unit test case how i can writ kindly help me.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonTypeName("ProcessMessageDedup")
public class ProcessMessageDedup extends ProcessMessage {
    @ConfigProperty(name = "dedupAppName", defaultValue = "dedup")
    public String appName;
    @ConfigProperty(name = "dedupProcessURL")
    public String processURL;
    @ConfigProperty(name = "callbackURL")
    public String callbackURL;
    @ConfigProperty(name = "dedupCallbackEndpoint")
    public String callbackEndpoint;
    @ConfigProperty(name = "callbackUser")
    public String callbackUser;
    @ConfigProperty(name = "callbackPass")
    public String callbackPass;

    public ProcessMessageDedup() {
        setProperties();
    }

    private void setProperties() {
        appName = ConfigProvider.getConfig().getValue("dedupAppName", String.class);
        processURL = ConfigProvider.getConfig().getValue("dedupProcessURL", String.class);
        callbackURL = ConfigProvider.getConfig().getValue("callbackURL", String.class);
        callbackEndpoint = ConfigProvider.getConfig().getValue("dedupCallbackEndpoint", String.class);
        callbackUser = ConfigProvider.getConfig().getValue("callbackUser", String.class);
        callbackPass = ConfigProvider.getConfig().getValue("callbackPass", String.class);
    }

    public void process() throws Exception {
        try {
            logger.info("Processing " + appName + " tid: " + tid);
            logger.info("Processing " + appName + " queryparams: " + queryParams.toString());
            setProperties();
            new CallbackUtils().callProcess(this.getData(), this.processURL, tid, fileId, "", queryParams, appName,
                    callbackURL, callbackEndpoint, callbackUser, callbackPass);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw e;
        } finally {
            this.deleteFile();
        }
    }
  
    @Override
    public void callback() {
        // TODO Auto-generated method stub

    }
}

please let me know how we can write a unit testing of this class.

ajaynmd
  • 9
  • 4
  • I am guessing that you are using spring? What do you want to test exactly? In normal tests I would simply mock the class. Wehnn you really what to test this class specifically with all of it´s configurations you need to write a spring boot test and read the config from the test application.properties in this context. – GJohannes May 17 '22 at 06:27
  • -GJohannes , can you please give me example how i can write unit test case for above given code. or how i can remove this class from code coverage of sonarqube. – ajaynmd May 17 '22 at 06:55
  • I have used mockito library to mock the ConfigProvider.getConifg.getValue using this way: pls follow this link: https://stackoverflow.com/a/74900447/997801 – Ramgau Dec 23 '22 at 14:02

2 Answers2

0

First of, what framework are you using, Spring, ...? In case of Spring, you want to be looking at @ConfigurationProperties and @Value.

To test config parameters in spring you should be using ReflectionTestUtils, see this answer for more info: https://stackoverflow.com/a/17355595/3845362

Secondly, it's not clear from you question what exactly it is you want to test.

  • Do you want to test the framework loading in the right values from application.properties?
  • Do you want to test logic inside your ProcessMessageDedup class?

Btw, your fields are public, so even after you initialize your class you could modify then as follows:

ProcessMessageDedup p = new ProcessMessageDedup (); // calls setProperties()
p.appName = "Foo";
p.processURL = "Bar";
...
Edito
  • 3,030
  • 13
  • 35
  • 67
0

@ConfigProperty is intended to be used by a CDI Container to inject the configuration value. A simple JUnit test is not enough to inject such values. You need a CDI container that handles the injection part.

You can either use Weld-JUnit5: https://github.com/weld/weld-testing/tree/master/junit5

Or in this case, since you are using Quarkus, you annotate your test class with @QuarkusTest and Quarkus will handle the injection part for you in the test class: https://quarkus.io/guides/getting-started-testing

Roberto Cortez
  • 803
  • 4
  • 8