单元测试 MapReduce - Junit Mockito
Unit Test MapReduce - Junit Mockito
我是为 Map Reduce 编写测试用例的新手,当我用谷歌搜索时,我了解到 MRUnit 已被弃用,必须使用 Mockito。有人可以提供有关使用 Junit mockito 测试 mapreduce 的灵感,因为我找不到。我只能看到 mapreduce 的 MRUnit 测试用例。
我在此处为映射器提供示例测试 class。 reducer 的测试也可以用同样的方式编写。
@RunWith(MockitoJUnitRunner.class)
public class SampleMapperTest {
@Mock
private Mapper.Context mockContext; // declare your mocks
@Mock
Counter mockCounter; // mocked hadoop counter
SampleMapper mapper;
@Before
public void setUp() throws Exception {
/*
* mock and define your components here
*/
mapper = new SampleMapper();
doNothing().when(mockCounter).increment(anyLong());
}
@Test
public void testMap() throws IOException, InterruptedException {
/*
* whatever you want to test you can write here in the verify statement
*/
String line1 = "key_value";
mapper.map(null, new Text(line1), mockContext);
verify(mockCounter, times(1)).increment(1);
}
@After
public void tearDown() throws Exception {
/*
* this will do the clean up part
*/
verifyNoMoreInteractions(mockContext);
}
}
我希望你能从中得到一些启发,现在应该可以编写你的测试了。
我是为 Map Reduce 编写测试用例的新手,当我用谷歌搜索时,我了解到 MRUnit 已被弃用,必须使用 Mockito。有人可以提供有关使用 Junit mockito 测试 mapreduce 的灵感,因为我找不到。我只能看到 mapreduce 的 MRUnit 测试用例。
我在此处为映射器提供示例测试 class。 reducer 的测试也可以用同样的方式编写。
@RunWith(MockitoJUnitRunner.class)
public class SampleMapperTest {
@Mock
private Mapper.Context mockContext; // declare your mocks
@Mock
Counter mockCounter; // mocked hadoop counter
SampleMapper mapper;
@Before
public void setUp() throws Exception {
/*
* mock and define your components here
*/
mapper = new SampleMapper();
doNothing().when(mockCounter).increment(anyLong());
}
@Test
public void testMap() throws IOException, InterruptedException {
/*
* whatever you want to test you can write here in the verify statement
*/
String line1 = "key_value";
mapper.map(null, new Text(line1), mockContext);
verify(mockCounter, times(1)).increment(1);
}
@After
public void tearDown() throws Exception {
/*
* this will do the clean up part
*/
verifyNoMoreInteractions(mockContext);
}
}
我希望你能从中得到一些启发,现在应该可以编写你的测试了。