I have a class that take a ArrayList as parameter:
public class Foo {
private ArrayList<Bar> bars;
public Foo(ArrayList barList) {
bars = barList;
}
}
there is a bug that I can pass any ArrayList into the constructor:
// should compile error with this line
Foo foo = new Foo(new ArrayList<String>());
the problem is if I add this case to test suite, when the bug fixed, I can not compile it. is there anyway to test this case?
I don't know of any language/unit test framework that will let you "test" for code that should not compile. If you can't compile it, there is nothing to test. You can however turn on all compiler warnings when building. I'm pretty sure passing an unparameterized collection is a big warning in any JVM after JDK5.
I feel it is bad practise and I don't really see a use for it, but see this example for how to test for compilations errors:
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class CompileTest {
//A very naive way checking for any error
@Test(expected=java.lang.Error.class)
public void testForError() throws Exception {
this.is.not.a.statement;
}
//An example which check that we only find errors for regarding compilation errors.
@Test
public void expectNotCompilableMethod() {
try {
uncompilableMethod();
fail("No compile error detected");
} catch (Error e) {
assertTrue("Check for compile error message", e.getMessage().startsWith("Unresolved compilation problems"));
}
}
private void uncompilableMethod() {
do.bad.things;
}
}
Edit: 1) I am not sure how this may behave together with build-tools like maven. As far as I know maven breaks the build at compile-errors, so probably the tests will not even be executed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With