Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assert that an expression does not compile

Tags:

java

junit

I have written a piece of framework that adds the possibility for type-safe invocations of its interface. Now, when writing the JUnit tests, I want to show that specific expressions that earlier led to runtime errors are checked by the compiler now.

// this does not compile, because nameProp is of type Property<String>
Integer name = interface.getProperty(nameProp);

Probably it'd be best to simply comment that code out and leave it like that. I was just wondering whether it'd be possible with some testing framework to write something like

assertCompilationError() {
     Integer name = interface.getProperty(nameProp);
}

I explicitly do not want to fiddle around with invocations of javac with a custom classpath myself. If there is the possibility for a general solution that could be extracted to framework code (and donated to JUnit or TestNG) such a solution would be welcome as well.

like image 346
sorencito Avatar asked Jan 09 '13 12:01

sorencito


1 Answers

You can't run JUnit until it compiles which is too late for this check.

You can't use a library to allow code which doesn't compile, to compile so you can check it doesn't compile.

like image 96
Peter Lawrey Avatar answered Sep 24 '22 05:09

Peter Lawrey