对于我的Map Reduce工作,我打算使用Junit和Mockito而不是MRUnit(因为已弃用)
我从WordCount示例开始,我希望首先测试我的映射器。作为映射器的一部分,我想基于MAP_OUTPUT_RECORDS计数器进行验证。这是我的下面带有错误的示例代码
@RunWith(MockitoJUnitRunner.class)
public class WordCountMapperTest {
@Mock
private Mapper.Context mockContext; // declare your mocks
org.apache.hadoop.examples.WordCount.TokenizerMapper mapper;
@Before
public void setUp() throws Exception {
/*
* mock and define your components here
*/
mapper = new WordCount.TokenizerMapper();
doNothing().when(mockContext.getCounter(TaskCounter.MAP_OUTPUT_RECORDS)).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(mockContext.getCounter(TaskCounter.MAP_OUTPUT_RECORDS), times(1)).increment(1);
}
}
在doNothing().when(mockContext.getCounter(TaskCounter.MAP_OUTPUT_RECORDS)).increment(anyLong());
行,它期望thenReturn()
,但是increment(anyLong())
是无效方法。我不明白为什么会这样。以下是我得到的错误
org.mockito.exceptions.misusing.UnfinishedStubbingException: 在此处检测到未完成的存根: -> com.hadoop.WordCountMapperTest.setUp(WordCountMapperTest.java:38)
例如thenReturn()可能会丢失。正确存根的示例: when(mock.isOk())。thenReturn(true); when(mock.isOk())。thenThrow(exception); doThrow(exception).when(mock).someVoidMethod();提示: 1.缺少thenReturn() 2.您正在尝试使用最终方法,您这个调皮的开发人员! 3:您之前已在存根另一个模拟的行为 如果完成则“ thenReturn”指令
在com.hadoop.WordCountMapperTest.setUp(WordCountMapperTest.java:38) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)处 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:238)在 org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)在 org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)在 org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)在 org.junit.runners.ParentRunner.run(ParentRunner.java:309)在 org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) 在 org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 在org.junit.runner.JUnitCore.run(JUnitCore.java:160)处 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)