Return-Path: Delivered-To: apmail-hadoop-general-archive@minotaur.apache.org Received: (qmail 70034 invoked from network); 27 May 2010 08:34:41 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 27 May 2010 08:34:41 -0000 Received: (qmail 25430 invoked by uid 500); 27 May 2010 08:34:40 -0000 Delivered-To: apmail-hadoop-general-archive@hadoop.apache.org Received: (qmail 25126 invoked by uid 500); 27 May 2010 08:34:37 -0000 Mailing-List: contact general-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: general@hadoop.apache.org Delivered-To: mailing list general@hadoop.apache.org Received: (qmail 25118 invoked by uid 99); 27 May 2010 08:34:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 May 2010 08:34:37 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=SPF_PASS,TO_NO_BRKTS_DIRECT X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [193.252.149.37] (HELO smtpout01s1.x-echo.com) (193.252.149.37) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 27 May 2010 08:34:29 +0000 X-Spam-Level: Message-ID: <4BFE3004.5000603@echo.fr> Date: Thu, 27 May 2010 10:40:36 +0200 From: Varene Olivier User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: general@hadoop.apache.org Subject: Mapper Reducer : Unit Test and mocking with static variables Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AV-Checked: ClamAV using ClamSMTP X-Virus-Checked: Checked by ClamAV on apache.org X-Old-Spam-Status: No, score=-4.3 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.3 Hello to all, first of all many thanks for this great piece of software you are all contributing to :) I am actually creating a program in Hadoop MapReduce, but I intend to massively use JUnit Tests to validate my code. To test a Mapper, I mock a Mapper.Context object and run the Mapper.map function with it, but inside my Mapper.map function, I am using Counters to maintain internal statistics. The update of those counters make the test fails with a null pointer (JavaNullPointerException). There is an issue "Counters" of variable visibility from my test ... Do you have any idea how to mock/validate/overcome this issue ? Thanks a lots Best Regards Olivier Varene : Code : -------- private static enum Counters {NBLINES}; private static IntWritable one = new IntWritable(1); private static LongWritable oneL = new LongWritable(1); // Mapper /** * outputs 1 for each line encountered */ public static class LCMapper extends Mapper { private final static IntWritable one = new IntWritable(1); private final static LongWritable oneL = new LongWritable(1); /** * for each line encountered outputs 1 for the key 1 */ public void map(LongWritable p_key, Text p_inputs, Context p_context) throws IOException, InterruptedException { p_context.getCounter(Counters.NBLINES).increment(1); p_context.write(one,oneL); } // end map } // end Mapper : Test : -------- @Test public void testLCMapper() throws IOException, InterruptedException { LCMapper mapper = new LCMapper(); Text value = new Text("ligneA\nlignB\n"); // mock the map job execution context Context context = mock(Context.class); try { mapper.map(null, value, context); } catch (NullPointerException e) { // exception raised by context.getCounter(...) null pointer // HOW TO MOCK ??? // Counter is a static enum from the englobing class ... // issue with variable visibility (from map function) in this test } // verify that the Mapper issued (one,oneL) pair verify(context).write(one,oneL); } // end testLCMapper