1

I have a cucumber JVM + Selenium scripts. The main method is something like this,

public static void main (String args[]) {

    environment.environmentValue = args[0];
    path.pathValue = args[1];
    username.superUser = args[2];

        returnCode = Main.run(
            new String[] { "-g", "com.sanity.step.definition","-t", "@" +path ,
                        featureFile.replace("\\", "\\\\") },
                SeparateClassloaderTestRunner.class.getClassLoader());
}

I have a constructor class, i.e

public Class CucumberRunner;

private ClassUtility environment;
private Classutility pathVal;
private ClassUtility userName;


    public CucumberRunner(ClassUtility environment , ClassUtility pathVal, 
      ClassUtility userName) {
            this.environment = environment;
            this.pathVal=pathVal;
            this.userName= userName;

        }

This is my classutility Class,

public class ClassUtility {

    public String environmentValue;
    public String pathValue;
    public String superUser;
}

I am getting the compilation error as "Cannot make a static reference to the non-static field method in main method "enviornment.environmentValue = args[0]". So how do we do dependency injection for static variables? Can we do dependency injection for runtime arguments?

Mathan
  • 162
  • 1
  • 2
  • 14
  • This answer may help you : https://stackoverflow.com/questions/11324372/how-to-make-spring-inject-value-into-a-static-field – Thoomas Jul 25 '17 at 11:50
  • @Thoomas, How does it work with runtime arguments? In my case, the arguments from main method has to be injected. Can you please let me know how to do it? – Mathan Jul 25 '17 at 13:28
  • Spring Boot can handle JVM arguments for you. You can retrieve them by doing: `@Value("${argumentName}") private String var`. If this is option suits your needs, I can provide a concrete example as an answer. – Thoomas Jul 25 '17 at 13:34
  • Thank you Thoomas, My application doesn't use Spring Boot. It just uses only dependency injection of spring (PICO Container). So it will not be helpful to me. – Mathan Jul 25 '17 at 13:50
  • @Thoomas - Can you provide me the spring boot example. Thanks in advance!! – Mathan Aug 07 '17 at 11:02
  • See my answer if it's helpful for you or if you need more details. – Thoomas Aug 07 '17 at 20:17

1 Answers1

1

Spring Boot can handle JVM arguments for you.

You can inject your parameters as VM arguments when launching the Spring Boot App.

For example, java -jar yourboot.jar -DargumentName=test. You can retrieve the value by doing : @Value("${argumentName}") private String var in your spring beans (controllers, services, ...).

Thoomas
  • 2,150
  • 2
  • 19
  • 33