でも、やはり0.19.xが前提なので情報が古い。
ってことで、0.20.xで以下のような感じで書いてみた。
package example.test;
import java.util.Arrays;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import static org.mockito.Mockito.*;
import junit.framework.TestCase;
public class CountTest extends TestCase {
@SuppressWarnings("unchecked")
public void testMapLongWritableTextContext() {
// Mapのテスト
Map map = new Map();
Mapper.Context map_context = mock(Mapper.Context.class);
Text value = null;
try {
value = new Text("test1");
map.map(null, value, map_context);
verify(map_context, times(1)).write(new Text("test1"), new LongWritable(1));
value = new Text("test2");
map.map(null, value, map_context);
verify(map_context, times(0)).write(new Text("test1"), new LongWritable(0));
} catch (Exception e) {
fail(e.getMessage());
}
// Reduceのテスト
Reduce reduce = new Reduce();
Reducer.Context red_context = mock(Reducer.Context.class);
Text key = null;
Iterable<longwritable> values;
try {
key = new Text("test1");
values = Arrays.asList(new LongWritable(2));
reduce.reduce(key, values, red_context);
verify(red_context, times(1)).write(new Text("test1"), new LongWritable(2));
key = new Text("test2");
values = Arrays.asList(new LongWritable(2), new LongWritable(2));
reduce.reduce(key, values, red_context);
verify(red_context, times(1)).write(new Text("test2"), new LongWritable(4));
} catch (Exception e) {
fail(e.getMessage());
}
}
}
0 件のコメント:
コメントを投稿