Use case

Normally if we like to create a utility class we normally implement it with static methods.

Problem

This is good if we can write unit tests on it. But most of the time this is not the case since it is difficult to write a unit on it. Specially, if the utility class that have a lots of static methods inside that depends on each other. For example, the following piece of code (i.e. Code 1) even though small authoring a test could be difficult.

Code 1. Small class that cannot be tested easily.

package example

uses java.lang.Math

class SampleCodeWithStaticMethods {

private construct() {}

static function myMethod() {
 var value = someAction() + 10
 print(value)
 }

static function someAction() : int {
 return (Math.round(Math.random()*100) as int) + 100
 }

}

Imagine write a unit test for myMethod. It is almost impossible because the method depends on someAction which is also a static method that returns random integer.

We need to refactor this to make it testable. The first thing we can do is identify if there are other static methods being called that are not a member of the class and make a separate non-static method for them. From code 1, those static method calls are:

- Print
- Math.round
- Math.random

Call those new member instance methods in place of the static method call. Making them contained in a separate method allows us to bypass them by overriding on a child class (i.e. basic OOP principle).

The next thing is remove all the static modifiers to convert all of them to member instance methods.

The refactored code would be like the following code.

Code 2. Refactored to use non-static methods

package example
uses java.lang.Math

class SampleCodeWithoutStaticMethods {

//Contains the call the to static print method.
 function output(value : int) {
 print(value)
 }

//Contains the call the Math.round and Math.random.
 function random() : int {
 return (Math.round(Math.random()*100) as int)
 }

function myMethod() {
 var value = someAction() + 10
 //Calls the member method that contains the static print method.
 output(value)
 }

function someAction() : int {
 return random() //Calls the member method that contains the static Math.round and Math.random method.
 + 100
 }

}

Now testing myMethod becomes so trivial (i.e. Code 3) using the very old behaviour of OOP (i.e. being polymorphic). The key is the inner class SampleCodeWithoutStaticMethodsMyMethod where we overrode someAction and output instance methods.

The someAction method now just return 25 which makes it more consistent than its original random behaviour.

The output method is just collecting what's being passed to it to result field instead of printing it.

Code 3. Unit test for myMethod

package example

uses gw.api.system.server.Runlevel
 uses gw.testharness.RunLevel

@gw.testharness.ServerTest
 @RunLevel(Runlevel.NONE)
 class SampleCodeWithoutStaticMethodsTest extends gw.testharness.TestBase {

class SampleCodeWithoutStaticMethodsMyMethod extends SampleCodeWithoutStaticMethods {
 var result : int

override function someAction() : int {
 return 25
 }

override function output(value : int) {
 result = value
 }
 }

function testMyMethod() {
 var testObj = new SampleCodeWithoutStaticMethodsMyMethod()
 testObj.myMethod()
 assertEquals(35, testObj.result)
 }

}