weiclin
weiclin

Reputation: 95

how to unit test code that should cause compile error

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?

Upvotes: 6

Views: 2727

Answers (3)

Giovanni Botta
Giovanni Botta

Reputation: 9816

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.

Upvotes: 3

Denis Lukenich
Denis Lukenich

Reputation: 3164

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.

Upvotes: 3

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10288

Fix your method signature:

public Foo(ArrayList<Bar> barList) {
    bars = barList;
}

Problem solved. (You probably also want to check for null.)

Upvotes: 0

Related Questions