Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing when the output is a wall of text

I am currently testing a part of my application (that we will assume is a class named X) that takes as parameter a reflection java Method, and will print some java code based on it (that is, it is a code generator). For instance, when passing String.toString() to X, the rendered string output would be:

public static java.lang.String toString(String thisObj, boolean isMonitoring) {
    String thisObjOld = (String)thisObj.clone(thisObj, false);

    if (isMonitoring) {
        toStringPre(thisObj);
    }

    java.lang.String result = thisObj.toString_Original();

    if (isMonitoring) {
        toStringPost(thisObj, thisObjOld, result);
    }

    return result
}

Now, I am wondering how to test this output. I'd like to test different things (individually).

I know the perfect thing to do would be to make assertions against an abstract representation, just before the printing, instead of the final text. But unfortunately, it seems that would be just too much work to set up.

Examples of things I'm looking to test are:

  • If the return type is correct (java.lang.String);
  • If the method's name is correct (toString), as well as its parameters `(String thisObj, boolean isMonitoring).

How to better approach this? Using regular expressions? Using String.split()?

like image 884
devoured elysium Avatar asked Feb 01 '26 20:02

devoured elysium


1 Answers

What about compiling the generate code - Test that the code is syntactic correct.

Then use Reflection to check the Name, Parameter and Return type.

Then run it in a Mocked environment to check that it does what it should do.

like image 131
Ralph Avatar answered Feb 04 '26 08:02

Ralph