When static methods should be used in Custom Keywords?

One more example. This would give you an insight how CustomKeywords work. This is meant for an expert Groovy programmers.

I made a Keyword class my.KwPrivateConstructorAndStaticMethod:

package my

public class KwPrivateConstructorAndStaticMethod {

	private KwPrivateConstructorAndStaticMethod() {
		println KwPrivateConstructorAndStaticMethod.class.getName() + " was constructed"
	}

	static String greeting(String name) {
		return "Hello, ${name}!"
	}
}

I made a Test Case TC3:

println CustomKeywords."my.KwPrivateConstructorAndStaticMethod.greeting"('Paul')

When I ran the TC3, I got:

2021-02-15 10:41:47.044 DEBUG testcase.TC3                             - 1: println(my.KwPrivateConstructorAndStaticMethod.greeting("Paul"))
2021-02-15 10:41:47.080 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - ❌ Class com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass can not access a member of class my.KwPrivateConstructorAndStaticMethod with modifiers "private"
2021-02-15 10:41:47.085 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/TC3 FAILED.
Reason:
java.lang.IllegalAccessException: Class com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass can not access a member of class my.KwPrivateConstructorAndStaticMethod with modifiers "private"
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:46)
	at TC3.run(TC3:1)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:398)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:389)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:368)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:360)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:255)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1613353293369.run(TempTestCase1613353293369.groovy:25)

An Exeption was raised at at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:46). You can read the source code at

If you read the source code carefully at Line#46 - 50, you can find the following points.

  1. Line#46: CustomKeywordDelegatingMetaClass calls the constructor of the Keyword class to create an instance of the Keyword class. The caller requires a public constructor with no arguments. In the case of my.KwPrivateConstructorAndStaticMethod class, it lacks a public constructor; therefore an Exception is raised.
  2. Line#50: CustomKeywordDelegatingMetaClass invokes a method of name customKeywordMethodName regardless if the method is static or non-static.
  3. Line#36: The method is named as invokeStaticMethod. But I think the method should rather be named as invokeMethod, because the method can actually deal with both cases where the method is with or without static modifier.

I guess that the Katalon programmer who originated this code had static modifier in mind, but he/she eventually wrote something slightly different from the original idea.

===================

Let me write the last possible case. Another Keyword class with a private constructor and a public instance method (non-static method):

package my

public class KwPrivateConstructorAndNonstaticMethod {

	private KwPrivateConstructorAndNonstaticMethod() {
		println KwPrivateConstructorAndStaticMethod.class.getName() + " was constructed"
	}

	String greeting(String name) {
		return "Hello, ${name}!"
	}
}

A Test Case TC4:

import my.KwPrivateConstructorAndNonstaticMethod as kw
println kw.greeting('George')

When I ran TC4, I got error (as I expected):

2021-02-15 15:40:18.199 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC4
2021-02-15 15:40:19.266 DEBUG testcase.TC4                             - 1: println(KwPrivateConstructorAndNonstaticMethod.greeting("George"))
2021-02-15 15:40:19.312 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/TC4 FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static my.KwPrivateConstructorAndNonstaticMethod.greeting() is applicable for argument types: (java.lang.String) values: [George]
Possible solutions: greeting(java.lang.String), getAt(java.lang.String)
	at TC4.run(TC4:2)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
....

This class (with a private constructor and a public instance method) never works. It is just useless.

1 Like