mrunit-dev mailing list archives

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

    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
MultipleOutputs.

    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.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

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.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.StringTokenizer;


@RunWith(PowerMockRunner.class)

@PrepareForTest({MultipleOutputs.class, MockMapContextWrapper.class,
MockOutputCreator.class})

public class TestMultipleOutput {


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

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


    @Before

    public void setup() throws Exception {

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

        List<Pair<Text, IntWritable>> inputs = new ArrayList<Pair<Text,
IntWritable>>();

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

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

        PowerMockito.whenNew(MultipleOutputs.class)

                .withArguments(context.getMockContext())

                .thenReturn(mos);

        mapper = new Map();

        mapDriver = MapDriver.newMapDriver(mapper);

    }


    @Test

    public void TestMapDriver() throws IOException {

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

                .withOutput(new Text("hello"), new
IntWritable(1)).runTest();

    }


    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;


        @Override

        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");

            }

            super.setup(context);

        }


        @Override

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

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

            super.cleanup(context);

        }


        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()) {

                word.set(tokenizer.nextToken());

                context.write(word, one);

            }

        }

    }


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


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

            super(context);

        }


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

            return;

        }


    }

}

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