單元測試出現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

  • 目錄結構

-java apache

    -v1
app

        -MaxTemperatureMapper.java oop

        -MaxTemperatureMapperTest.java
spa

  • 編譯

#cd java/v1 #package v1; .net

#javac MaxTemperatureMapperTest.java MaxTemperatureMapper.java code


  • 運行
 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)

解決方法: hadoop

出現這樣的問題是因爲對java不熟悉,參考 get

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

因爲在當前目錄(v1)中沒有包v1,因此應該將當前目錄設置爲包v1所在的目錄(java)

cd ..

hadoop org.junit.runner.JUnitCore v1.MaxTemperatureMapperTest

結果:

JUnit version 4.5 .I. Time: 0.643 OK (1 test)

相關文章
相關標籤/搜索