How can I test a class like this (see code below)?
public final class A {
public static final String FIRST = "1st";
public static final String SECOND = "2nd";
private A() {
// NOP
}
}
For now all my coverage tools say that constructor isn't covered with tests. My tests look like on this:
assertEquals(A.FIRST, "1st");
assertEquals(A.SECOND, "2nd");
How can I test my class?
UPD
This code solved my problem.
@Test
public void magic() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Constructor<A> constructor = A.class.getDeclaredConstructor();
constructor.setAccessible(true);
A instance = constructor.newInstance();
assertNotNull(instance);
}
Yes, I agree that this isn't the best solution. But it works :)
Reflection is probably the way to go: How do I test a class that has private methods, fields or inner classes?
By the way, this is possibly a duplicate of the question in the link provided.
Alternatively, could you create a wrapper method that is protected
which simply forwards all calls to the private
method?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句