views:

53

answers:

2

I made a little app to generate Java test classes based on certain set of information. Basically, I am generating the boilerplate code needed for a library that we are using.

I would like to test the generated code in better way than just comparing the output to an expected string.

  • Is there any facility to test if the Java code contains errors?
  • Is there any facility to determine if the Java file will compile?
  • Is there a strategy that I should use for this kind of situation (I was thinking of using Class class to get information about the class)?
+2  A: 
  • Is there any facility to test if the Java code contains errors?

Yes - the java compiler.

  • Is there any facility to determine if the Java file will compile?

Yes - the Java compiler.

  • Is there a strategy that I should use for this kind of situation?

Yes - compile the file.

Note that you can call the Java compiler at runtime, either using Runtime.exec(...) or within the current JVM. Here's a decent page on calling javax.tools dynamically within the current JVM.

Stephen C
+1  A: 

Java 6 introduced the Compiler API. This can be used to to compile Java code without calling an external compiler. Here's a short introduction.

Andreas_D