mrunit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hahn jiang <>
Subject problem for MultipleOutputs
Date Wed, 17 Jul 2013 07:57:39 GMT

    I write a simple example to mock MultipleOutputs use PowerMock. I write
a MockMultipleOutputs class only have constructor and write(). In this test
case, i use MockMapContextWrapper.class and MockOutputCreator.class to mock

    when i run this test case, the mos in the setup of Map is null, so it
will throw NullPointException in map() function.

I want to know why my mos is null, when i use PowerMockito.whenNew.

thank you

package org.apache.hadoop.mrunit.mapreduce;

import org.apache.hadoop.conf.Configuration;




import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.TaskInputOutputContext;

import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

import org.apache.hadoop.mrunit.internal.mapreduce.MockMapContextWrapper;

import org.apache.hadoop.mrunit.internal.output.MockOutputCreator;

import org.apache.hadoop.mrunit.types.Pair;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.powermock.api.mockito.PowerMockito;

import org.powermock.core.classloader.annotations.PrepareForTest;

import org.powermock.modules.junit4.PowerMockRunner;


import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;


@PrepareForTest({MultipleOutputs.class, MockMapContextWrapper.class,

public class TestMultipleOutput {

    private Mapper<LongWritable, Text, Text, IntWritable> mapper;

    private MapDriver<LongWritable, Text, Text, IntWritable> mapDriver;


    public void setup() throws Exception {

        MockOutputCreator<Text, IntWritable> mockOutputCreator = new
MockOutputCreator<Text, IntWritable>();

        List<Pair<Text, IntWritable>> inputs = new ArrayList<Pair<Text,

        MockMapContextWrapper context = new MockMapContextWrapper(new
Configuration(), inputs, mockOutputCreator, new MapDriver());

        MultipleOutputs<Text, IntWritable> mos = new




        mapper = new Map();

        mapDriver = MapDriver.newMapDriver(mapper);



    public void TestMapDriver() throws IOException {

        mapDriver.withInput(new LongWritable(0), new Text("hello"))

                .withOutput(new Text("hello"), new


    public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {

        private final IntWritable one = new IntWritable(1);

        private Text word = new Text();

        private MultipleOutputs<Text, IntWritable> mos = null;


        public void setup(Context context) throws IOException,
InterruptedException {

            if (mos == null) {

                System.out.println("before mos is null");

            } else {

                System.out.println("before mos is not null");


            mos = new MultipleOutputs<Text, IntWritable>(context);

            if (mos == null) {

                System.out.println("after mos is null");

            } else {

                System.out.println("after mos is not null");





        public void cleanup(Context context) throws IOException,
InterruptedException {

            if (mos != null) mos.close();



        public void map(LongWritable key, Text value, Context context)

                throws IOException, InterruptedException {

            if (mos == null) {

                System.out.println("mos is null");

            } else {

                System.out.println("mos is not null");


            mos.write("test", new Text(""), new IntWritable(0));

            String line = value.toString();

            StringTokenizer tokenizer = new StringTokenizer(line);

            while (tokenizer.hasMoreTokens()) {


                context.write(word, one);




    class MockMultipleOutputs<KEYOUT, VALUEOUT> extends
MultipleOutputs<KEYOUT, VALUEOUT> {

        public MockMultipleOutputs(TaskInputOutputContext<?, ?, KEYOUT,
VALUEOUT> context) {



        public <K, V> void write(String outputName, K key, V value) {





  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message