单元测试出现ClassNotFoundException错误(已解决)

//MaxTemperatureMapperTest.java

package v1;
// cc MaxTemperatureMapperTestV1 Unit test for MaxTemperatureMapper
// == MaxTemperatureMapperTestV1Missing
// vv MaxTemperatureMapperTestV1
import static org.mockito.Mockito.*;

import java.io.IOException;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.OutputCollector;
import org.junit.*;

public class MaxTemperatureMapperTest {

  @Test
  public void processesValidRecord() throws IOException {
    MaxTemperatureMapper mapper = new MaxTemperatureMapper();

    Text value = new Text("0043011990999991950051518004+68750+023550FM-12+0382" +
                                  // Year ^^^^
        "99999V0203201N00261220001CN9999999N9-00111+99999999999");
                              // Temperature ^^^^^
    OutputCollector<Text, IntWritable> output = mock(OutputCollector.class);

    mapper.map(null, value, output, null);

    verify(output).collect(new Text("1950"), new IntWritable(-11));
  }
}


//MaxTemperatureMapper.java

package v1;
// cc MaxTemperatureMapperV1 First version of a Mapper that passes MaxTemperatureMapperTest
import java.io.IOException;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
//vv MaxTemperatureMapperV1
public class MaxTemperatureMapper extends MapReduceBase
  implements Mapper<LongWritable, Text, Text, IntWritable> {

  public void map(LongWritable key, Text value,
      OutputCollector<Text, IntWritable> output, Reporter reporter)
      throws IOException {

    String line = value.toString();
    String year = line.substring(15, 19);
    int airTemperature = Integer.parseInt(line.substring(87, 92));
    output.collect(new Text(year), new IntWritable(airTemperature));
  }
}

运行步骤:

  • 目录结构

-java

    -v1

        -MaxTemperatureMapper.java

        -MaxTemperatureMapperTest.java

  • 编译

#cd java/v1 #package v1;

#javac MaxTemperatureMapperTest.java MaxTemperatureMapper.java


  • 运行
 hadoop org.junit.runner.JUnitCore MaxTemperatureMapperTest


  • 结果
JUnit version 4.5
Exception in thread "main" java.lang.NoClassDefFoundError: MaxTemperatureMapperTest (wrong name: v1/MaxTemperatureMapperTest)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:171)
        at org.junit.runner.JUnitCore.runMain(JUnitCore.java:79)
        at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:54)
        at org.junit.runner.JUnitCore.main(JUnitCore.java:46)

解决方法:

出现这样的问题是由于对java不熟悉,参考

http://stackoverflow.com/questions/4951695/receiving-wrong-name-noclassdeffounderror-when-executing-a-java-program-from-t

由于在当前目录(v1)中没有包v1,所以应该将当前目录设置为包v1所在的目录(java)

cd ..

hadoop org.junit.runner.JUnitCore v1.MaxTemperatureMapperTest

结果:

JUnit version 4.5
.I.
Time: 0.643


OK (1 test)


你可能感兴趣的:(单元测试出现ClassNotFoundException错误(已解决))