I have started using Karate and found it pleasing to use. As I am exploring, I am curious to know many things about it.
One of them being this:
So, I understand (like Cucumber) there are two ways to run:
1.) Using build tool(say Maven) where it’s surefire plugin will look for a runner file (with the prescribed *Test.java naming conventions) and this file would be annotated with (Karate.class). And the execution is transferred to Karate.
2.) IDE which creates Run Configurations (which is basically issuing java command with required classpath and invoking cucumber.api.cli.Main with required parameters related to glue, feature file/scenario line, tags etc. )
But a simple search on IntelliJ shows that there are two .class files on classpath with name – cucumber.api.cli.Main. One from cucumber jar file and the other from Karate.
My question is how is java able to figure out that it has to invoke the one from Karate and not the one from cucumber.
When I open Run configurations and open an option to choose Main class, it shows me just one when I search for it, as if it knows to ignore the cucumber one. So, even IntelliJ somehow knows this.
Advertisement
Answer
Yes, I’m responsible for this hack and not sure of the details, but it seems to work.
I think the simple rule is that the Java class-loader has a sequence / hierarchy – and the classes directly on the classpath are given preference, and any classes within a JAR file are given a lower preference. That’s all.
And when both come from a JAR file, the (Maven) dependency order matters.