In a test class, I’d like to provide my own overload of assertEquals with some special logic not relying on Object.equals. Unfortunately, that doesn’t work because as soon as I declare my assertEquals method locally, Java doesn’t find the static import from org.junit.Assert.* any more.
Is there a way around this? I.e. is there a way to provide an additional overload to a statically imported method? (The rather obvious solution being to name the method differently but this solution doesn’t have the same aesthetic appeal.)
My test class file looks something like this:
package org.foo.bar;
import static org.junit.Assert.*;
import org.junit.Test;
public class BarTest {
private static void assertEquals(Bar expected, Bar other) {
// Some custom logic to test equality.
}
@Test
public void testGetFoo() throws Exception {
Bar a = new Bar();
assertEquals(42, a.getFoo()); // Error *
}
@Test
public void testCopyConstructor() throws Exception {
Bar a = new Bar();
// Fill a.
Bar b = new Bar(a);
assertEquals(a, b);
}
}
Error * is “The method assertEquals(Bar, Bar) in the type BarTest is not applicable for the arguments (int, int).”