1

I have a Spring Boot application that launches the Karate tests programmatically, in the main source-set. This application is supposed to configure per environment several URLs that will be used by Karate. I also have a spring-cloud-config server that loads the yaml property file that corresponds to the current environment (dev, prod, etc). As such, my properties are not local to the project and are not readable outside of a Spring Boot property component.

With all that in mind, I want my Karate tests to use these configurable URLs, but I find no way to use the Spring Boot component containing my properties in a Karate context.

As further information, to run the Karate tests programmatically I'm using JUnitCore#runClasses :

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);

        // Run all tests in the application
        Result result = JUnitCore.runClasses(KarateApplicationRunner.class);  
    }
}  

with the KarateApplicationRunner being a runner launching the tests and generating a report.

If someone has a functioning idea it would make my day :) .

Lukas Cardot
  • 166
  • 1
  • 2
  • 12

1 Answers1

2

I really don't have experience with these particular Spring Boot test infra. But throwing out a few ideas.

In fact I find that Spring Boot introduces "too much magic" at times like this. So one approach I take is to use "java first principles" and introduce a prod bean whose only purpose is to make it easier for tests to grab info out of an app started by a unit test:

ServerStartedInitializingBean.java - and example use ServerStart.java

If you look at these tests, you may get more ideas: PaymentService.java - when you start a Spring Boot app using the main(String[] args) you have direct control over config.

If from your Spring Boot init code you can set a System.property or two that's sufficient to pass data to Karate.

Also see this answer: https://stackoverflow.com/a/45962035/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks a lot, your examples were helpful. In the end It was mostly me being hard-headed. My mind was stuck with the example in the doc setting a system property in a @BeforeClass... which can be done anywhere :D. I can just bind my properties to a component bean and pass it to karate via system properties. – Lukas Cardot Jul 05 '18 at 12:31