最近在学习Hadoop,在虚拟机上面的Centos系统下搭建了一个单机的Hadoop系统,对照《Hadoop权威指南》一边看一边运行里面的例子。因为大部分程序员都是用Eclipse来开发Hadoop程序,但我习惯于用命令行形式来写代码和运行程序。在Hadoop里面作单元测试时,用Eclipse网上却是有不少的实例,当如何在命令行下运行单元测试确实让我犯难了,难为了我好几天。晚上在Junit官网上看了相关资料,在Java环境下运行单元测试为: java
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] 程序员
将上面的命令行进行改造,移植到hadoop后为: apache
hadoop jar/usr/share/java/junit.jarorg.junit.runner.JUnitCore [test class name] ide
######################################################## oop
在本机测试记录以下: 单元测试
设置CLASSPATH
CLASSPATH包含hamcrest和junit的jar文件的完整目录,例如个人CLASSPATH为:
.:/opt/jdk1.6.0_45/lib:/opt/jdk1.6.0_45/jre/lib:/root/hadoop-1.2.0/hadoop-core-1.2.0.jar:/opt/jdk1.6.0_45/lib/junit-4.11.jar:/opt/jdk1.6.0_45/lib/hamcrest-core-1.3.jar
编译
// SingleResourceConfigurationTest.java
// == SingleResourceConfigurationTest
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.junit.Test;
public class SingleResourceConfigurationTest {
@Test
public void get() throws IOException {
// vv SingleResourceConfigurationTest
Configuration conf = new Configuration();
conf.addResource("configuration-1.xml");
assertThat(conf.get("color"), is("yellow"));
assertThat(conf.getInt("size", 0), is(10));
assertThat(conf.get("breadth", "wide"), is("wide"));
// ^^ SingleResourceConfigurationTest
}
}
javac SingleResourceConfigurationTest.java 学习
运行
hadoop
org.junit.runner.JUnitCore
SingleResourceConfigurationTest
结果
JUnit version 4.5
.
Time: 0.247
OK (1 test)