@devalex88
I tried writing JUnit tests for my custom Keywords in Katalon Studio and running the tests with Eclipse. I got a partial success, but encountered a difficulty. Let me report my research and ask for your support.
Successful case
I created a keyword Keywords/junittutorial/Calculator
. Please note that this keyword class is a Plain-Old-Java-Object which has no dependency upon the Katalon Studio’s runtime, e.g, the com.kms.katalon.core.configuration.RunConfiguration
object)
And I created a JUnit test for it Include/scripts/groovy/junittutorial/CalculatorTest.groovy
I started Eclipse photon, opened the project, and ran the JUnit test. The test ran successfully as expected.
Difficult case
I created another keyword Keywords/my/keywor/MyWebUI
. This keyword class tries to instantiate a ChromeDriver. This class has dependency upon Katalon Studio’s runtime.
I created another JUnit test Include/scripts/groovy/my/keyword/MyWebUITest.groovy
. This test simply invokes my.keyword.MyWebUI.openBrowser()
.
When I ran this test in Eclipse, got an Exception with the following statcktrace:
java.lang.NullPointerException
at com.kms.katalon.core.configuration.RunConfiguration.getDriverExecutionProperties(RunConfiguration.java:226)
at com.kms.katalon.core.configuration.RunConfiguration.getDriverSystemProperties(RunConfiguration.java:252)
at com.kms.katalon.core.webui.driver.DriverFactory.isUsingExistingDriver(DriverFactory.java:198)
at com.kms.katalon.core.webui.driver.DriverFactory.getExecutedBrowser(DriverFactory.java:853)
at com.kms.katalon.core.webui.driver.DriverFactory$getExecutedBrowser.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at my.keyword.MyWebUI.openBrowser(MyWebUI.groovy:22)
at my.keyword.MyWebUI$openBrowser.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at my.keyword.MyWebUITest.testOpenBrowser(MyWebUITest.groovy:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Why NullPointerException? It is easily seen. The keyword requires Katalon Studio runtime environment. But my JUnit test does not construct the runtime environment before invoking the keyword. Therefore the keyword can not run anyway.
My thought
Previously I ran my JUnit tests as a TestCase in Katalon Studio, for example see https://github.com/kazurayam/RunningJUnitInKatalonStudio/blob/0.2/Scripts/test/junittutorial.test/CalculatorTestRunner/Script1532666027182.groovy In this way, I could mix codes calling org.junit.* classes and com.kms.katalon.** classes with no problem.
Question
I want to write a custom keyword which has dependency on the Katalon Studio RunConfiguration. I want to do unit-testing for those keywords with JUnit. I want to run the JUnit tests with Eclipse. Then, how can I setup the Katalon runtime environment for each JUnit tests in Eclipse (not in Katalon Studio)?