This is a bit hard to explain, but the code should make it clear. I'm going through the Java koans (https://github.com/matyb/java-koans) to teach myself Java - they are fun by the way, I recommend them.
Anyway, the code has a bunch of assert statements you have to correct in order to complete the koans. Currently the two assert statements below are failing. I'm having trouble fixing the first one.
What's actually thrown is java.util.MissingFormatArgumentException
. So e.getClass() will return that. But it returns an actual Class object, not a string. To make the assert statement succeed, I think I need to get a Class object for java.util.MissingFormatArgumentException
. But I can't just create one because the Class class has no public constructor (see https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html).
Any ideas how to fix this assert? Simply removing it would be cheating. Fixing String.format() would also be cheating.
public void insufficientArgumentsToStringFormatCausesAnError() {
try {
String.format("%s %s %s", "a", "b");
fail("No Exception was thrown!");
} catch (Exception e) {
assertEquals(e.getClass(), "");
assertEquals(e.getMessage(), "");
}
}