tl;dr : Make sure your test class doesn't extend TestCase
.
I had a similar problem when I was using JUnit 4 with IntelliJ IDEA. I naïvely selected a base class of TestCase
in the dialog, which was the default for JUnit 3, because I figured "it'd be nice to have those handy this#assert*
methods" (the default for JUnit 4 is null). The bad code (which didn't work) is below:
public class SassCompilerTest extends TestCase {
@Rule
public ErrorCollector collector = new ErrorCollector();
@Test
public void testCompiler() throws IOException {
collector.checkThat(true, CoreMatchers.equalTo(false));
}
}
However, in JUnit 4, that prevented a lot of features from working. Removing the parent class fixed the test:
public class SassCompilerTest {
@Rule
public ErrorCollector collector = new ErrorCollector();
@Test
public void testCompiler() throws IOException {
collector.checkThat(true, CoreMatchers.equalTo(false));
}
}
The solution was suggested to me by a comment in the issue with Cucumber mentioned by @StefanBirkner in another answer. After reading that, I tried extending ErrorCollector
to make the ErrorCollector#verify
public and call it from an @After
method, but the @After
method wasn't getting called, which made me realize something was either wrong with the TestRunner (which was IntelliJ's default) or the Test itself.