2016-05-23 88 views
0

在TestNG(或JUnit)中,它很简单。云是这样的:在MRUnit中测试特定异常

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

上面的测试将通过自String xnull和一个NullPointerException异常。

但是在MRUnit中,断言的工作方式不同。下面是一个映射类的测试方法:

@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(); 

它需要一个输入与someData并预期与someKey的输出。但是我需要覆盖一个Try/Catch块,它通过提供坏数据来检查someData的有效性。在这种情况下,好像.withOutput方法甚至没有必要。 MRUnit有没有办法方便地测试Exceptions

回答

0

只需要做.run();而不是.runTest();。希望这可以帮助某人。