EvalMapper extends Mapper<LongWritable,Text,LongWritable,DoubleWritable> { @Override public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { Object candidate = StringUtils.fromString(value.toString()); double fitness = evaluator.getFitness(candidate, null); context.write(key, new DoubleWritable(fitness)); }  @Override void setup(Context context); @Override void map(LongWritable key,
                  Text value,
                  Context context); static final String MAHOUT_GA_EVALUATOR; }
@Test public void testMap() throws Exception { List<DummyCandidate> population = DummyCandidate .generatePopulation(POPULATION_SIZE); DummyEvaluator.clearEvaluations(); FitnessEvaluator<DummyCandidate> evaluator = new DummyEvaluator(); EvalMapper mapper = new EvalMapper(); Configuration conf = new Configuration(); conf.set(EvalMapper.MAHOUT_GA_EVALUATOR, StringUtils.toString(evaluator)); DummyRecordWriter<LongWritable,DoubleWritable> output = new DummyRecordWriter<LongWritable,DoubleWritable>(); Mapper<LongWritable,Text,LongWritable,DoubleWritable>.Context context = DummyRecordWriter.build(mapper, conf, output); mapper.setup(context); for (int index = 0; index < population.size(); index++) { DummyCandidate candidate = population.get(index); mapper.map(new LongWritable(index), new Text(StringUtils.toString(candidate)), context); } Set<LongWritable> keys = output.getKeys(); assertEquals("Number of evaluations", POPULATION_SIZE, keys.size()); for (LongWritable key : keys) { DummyCandidate candidate = population.get((int) key.get()); assertEquals("Values for key " + key, 1, output.getValue(key).size()); double fitness = output.getValue(key).get(0).get(); assertEquals("Evaluation of the candidate " + key, DummyEvaluator.getFitness(candidate.getIndex()), fitness, EPSILON); } }