Issue
I am using JUnit 4.12. The assert methods are not generic in nature. For instance, assertEquals method looks like:
static public void assertEquals(Object expected, Object actual) {..}
Why is it not like?
static public <T> void assertEquals(T expected, T actual) {..}
I felt need for generic method declaration for better compile time checking and IDE auto completion.
Solution
Having a generic method like this:
<T> void assertEquals(T expected, T actual) { /* ... */ }
gives you no type safety to avoid comparing unlike types: you can pass in anything to this method, since T
degenerates to its upper bound, Object
:
assertEquals("string", 0); // Compiles fine, even though they can't be equal.
And nor can you use any methods on expected
and actual
that aren't found on Object
. So, T
is basically just Object
.
As such, adding generics is just over-complicating the implementation.
Now, you could define a class like this:
class GenericAssert<T> {
void assertEquals(T expected, T actual) { /* ... */ }
}
and you could use this like:
new GenericAssert<String>().assertEquals("string", 0); // Compiler error.
because you've now placed a tighter upper bound on the acceptable parameters of assertEquals
, at class level.
But this just feels a bit awkward.
Answered By - Andy Turner
Answer Checked By - Terry (JavaFixing Volunteer)