2

This question could be thought of as just the Java version of this c++ question: Unit test compile-time error

I'm writing some utility classes that mess with generics in java and things have gotten weird enough that I find myself wishing I knew how to write a unit test for code that is supposed to cause a compiler error. I know how to make a unit test pass only when the expected exception is thrown. No idea how to make it pass only when I get the expected error. Is this possible? If so, how? Is there a typical way of doing this? Is there a jUnit way?

An example would be using a method (lets say, one that performs a cast) in a way that should cause an error. I'd like a test that would only pass when the code calling that method produces an error. I'd run it along with other tests that should not cause an error.

As another example read Generic And Parameterized Types and note how the comments often say things like "fails with ClassClassException" "should fail, but succeeds". Those seem like tests that could be automated.

This is not to test the compiler. It's to test that the generic code I wrote does what it should do.

My gut tells me this goes beyond what the compiler or libraries could do and is into the realm of IDE magic, strange project configuration, or static code analysis. I really have no idea. I just want to be just as sure that the code causes the errors it should as I am that it's not causing the errors it shouldn't. I use eclipse, ant, and jUnit so an answer that details how this could be done with them would be perfect.

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • 1
    If you are using JUnit, you can do this: `@Test(expected=NullPointerException.class)`. – Pokechu22 Sep 01 '14 at 06:34
  • Something like [this question](http://stackoverflow.com/q/156503/2970947)? – Elliott Frisch Sep 01 '14 at 06:34
  • That's a run time exception. I'm talking about compiler error's here. – candied_orange Sep 01 '14 at 06:35
  • 1
    Maybe run the compiler and check its return code? I'm rather curious what would be the purpose of a code that doesn't compile, though -- could you share your motivations behind this? – Jae Heon Lee Sep 01 '14 at 06:39
  • To prove that my code is causing the compiler errors it should rather than letting bad static casts get by that would later cause run time errors. – candied_orange Sep 01 '14 at 06:40
  • Can you give an example of a compiler error that you want to test? This will help make the discussion more concrete. – Code-Apprentice Sep 01 '14 at 06:49
  • @Code-Apprentice TreeMap hmAsTm = castToMapOf( String.class, String.class, new HashMap() ); – candied_orange Sep 01 '14 at 07:12
  • What is `castToMapOf()`? This looks like a method that will use reflection and thus create a **run time** exception, not a compiler error. – Code-Apprentice Sep 01 '14 at 07:19
  • @Code-Apprentice Something that casts a raw map to a generic Map but shouldn't allow you to use it as a TreeMap<> since it isn't one. This is just as an example. – candied_orange Sep 01 '14 at 07:24
  • @CandiedOrange Assuming a correct declaration for a `castToMapOf()` **method**, there is no **compiler error** in your example. (In fact, there's not even a cast, just a method call.) Please provide an example with a compiler error. – Code-Apprentice Sep 01 '14 at 07:29
  • @Code-Apprentice It actually causes - Type mismatch: cannot convert from Map to TreeMap and I want to be sure it keeps causing an error as I refactor it. – candied_orange Sep 01 '14 at 07:32
  • @CandiedOrange That compiler error is impossible for me to guess without the signature for `castToMapOf()`. – Code-Apprentice Sep 01 '14 at 07:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60370/discussion-between-candiedorange-and-code-apprentice). – candied_orange Sep 01 '14 at 07:52

1 Answers1

3

You can use another software to run some commands and check its results. It is then easy to check compile-time errors. It is possible to write it as a script in some language or as a separate project in java, but it must be distinct from your project.

Your commands to run should be javac, ant or any other depending on your IDE.

j123b567
  • 3,110
  • 1
  • 23
  • 32
  • My IDE is eclipse with Ant. It would be nice to automate this as part of the normal build even if it lives in another project. Have you ever seen this done? Can you to point to any examples? – candied_orange Sep 01 '14 at 23:19
  • ANTUnit can help you http://ant.apache.org/antlibs/antunit/ but I'm not familiar with ANT so I don't know more. – j123b567 Sep 16 '14 at 13:02