softvis-research / jqa-jira-plugin

This is a Jira parser for jQAssistant. It enables jQAssistant to scan and to analyze data from Jira.
GNU General Public License v3.0
5 stars 3 forks source link

[refactor] Find a better way for faking dependency injection #7

Open b-pos465 opened 4 years ago

b-pos465 commented 4 years ago

jQAssistant does not have a mechanism for dependency injection. This can make testing difficult.

The JiraRestClientWrapper is an abstraction layer which helps to mock requests to Jira while testing. The corresponding class is named MockedJiraRestClientWrapper. Unfortunately, there is no dependency injection in jQAssistant. Therefore, we use ENV to decide which implementation shall be used:

 if (System.getenv(TEST_ENV) != null) {
            return (JiraRestClientWrapper) GraphBuilder.class
                    .getClassLoader()
                    .loadClass("org.jqassistant.contrib.plugin.jira.jjrc.MockedJiraRestClientWrapper")
                    .newInstance();
        } else {
            return new DefaultJiraRestClientWrapper(url, username, password);
        }

This is obviously a bad solution. The ENV is set inside the JVM by the EnvironmentOverrider. The code has been taken from https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java.

Maybe using a system property could be a little bit cleaner. Maybe there is an even another solution which would be much better.

DirkMahler commented 4 years ago

https://github.com/softvis-research/jqa-jira-plugin/pull/10 replaces this by a system property taking the class name of the JiraRestClientWrapper implementation to use.