javahadoopmapreducemappermrunit

Testing for a specific exception in MRUnit


In TestNG (or JUnit), it's simple. Goes something like:

@Test(expectedExceptions = NullPointerException)
public void test() throws NullPointerException {
    String x = null;
    String y = "y";
    Assert.assertEquals(x.someMethod(), y);
}

Above test will pass since String x is null and a NullPointerException is thrown.

But in MRUnit, the assertion works differently. Following is a test method for a mapper class:

@Test(expectedExceptions = Data.InvalidDataException.class)
public void testFirstCatch() throws Exception {
    Data data = someData;
    MapDriver.newMapDriver(mapper)
        .withInput(new LongWritable(0), someData)
        .withOutput(someKey, NullWritable.get())
        .runTest();

It takes an input with someData and expects the output with someKey. But I need to cover a Try/Catch block where it checks for someData's validity by feeding bad data. In this case, it seems like .withOutput method isn't even necessary. Is there a way in MRUnit to conveniently test for Exceptions?


Solution

  • Just had to do .run(); instead of .runTest();. Hope this helps someone.