3

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()?

8
  • If the output is code, could you test that the generated code behaves correctly?
    – mpartel
    Oct 10, 2011 at 11:06
  • Theoretically yes, but not in practice. For this to work I'll have a way lot other methods that are not listed here. Oct 10, 2011 at 11:09
  • Could you explain why you want to test those things? Isn't this generated code used somewhere? A compiler checks those things if you use it...
    – duedl0r
    Oct 10, 2011 at 11:13
  • 1
    A compiler will not check that the code's semantic is correct. Oct 10, 2011 at 11:14
  • The generated code will be used somewhere else, but in a waaaaaaaaay complex environment. Plus, I'll only have the whole thing running in a couple of months, and I don't want to have to wait for it to test it! Oct 10, 2011 at 11:16

3 Answers 3

2

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.

1
  • Don't know when I'll have the full .java file working, so compiling it is not an option. Oct 10, 2011 at 16:18
1

I think if the generated code is very deterministic (and from looking at your question, it does look deterministic), then I would firstly hand write the correct string output from X, and do a string comparison.

E.g., a junit example:

public void testX(){
   X gnerator = new X(getTestMethodObject());
   Assert.assertEquals(StringUtils.readFully(new File("expected_output.txt")), generator.generate());
}

Obviously, that will perform string comparisons, and when the generated contents change, you'd have to hand write the new expected output. Hence, while this is simple, it could become a maintenance nightmare later on.

The other way, of course, is to lex the generated output and check the tokens generated (i m assuming you can't actually compile the generated code in your test env). Say, http://code.google.com/p/java-lexer/ which is a really simple lexor (i dont vouch for it - i just found it in a couple of minutes of googling...but it has some basic tests, so i assume its good?). Or, use something more complex like antlr which is probably overkill for testing.

I personally would stick with comparing hand written expected output with generated output. Its much lower key, and easier to spot errors than using full blown lexors.

2
  • The problem is that that way I'll have to write a lot more code than what I specifically want to test. I'll use this approach if nothing better comes along, but maybe even regex would be a better option than just plain old string comparison, imo. Oct 10, 2011 at 11:24
  • i suppose regex is one way to lex java code - after all, a lexer is just a really complicated statemachine, no different to a regex. I would've thought string comparison is going to be easier, because you can successively use the output of previous versions of the X class's generated output as a test, and hand edit the new changes in as they are added. This problem often happens in testing REST api's, and this solution is the one I find to be easiest and least trouble some for the amount of 'good' it gives.
    – Chii
    Oct 10, 2011 at 11:35
0

Why don't you 1) compile the generated code and use reflection to assert the "semantics", or, if compiling and loading is not possible due to dependencies and stuff, 2) use a JAVA parser to create an abstract syntax tree that you can traverse and make assertions. (The ANTLR project has grammars for JAVA 6, for example).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.