【Task1】
Develop the project “Hello World”
A .java program: Just print out “Hello” + your name;
A test case using Junit to verify whether the program works well.
1.在eclipse中新建project,命名为“Hello World”
2.在该项目上点右键-->properties-->Java Build Path-->Libraries-->Add Library
3.选择JUnit-->Next-->JUnit4-->Finish,现在已将JUnit4单元测试包引入了这个项目
4.在新建的project中新建class,命名为“HelloWorld”
5.在新建的类上点右键-->New-->JUnitTestCase,命名为“HelloWorldTest”
6.在HelloWorld.java中编写如下代码,其中创建了一种方法sayHello()
public class HelloWorld {
public String sayHello() {
return "Hello ZhangKexin!";
}
public static void main(String[] args) {
HelloWorld world = new HelloWorld();
System.out.println(world.sayHello());
}
}
7.在HelloWorldTest.java中编写如下代码,其中引入了assertEquals()函数
public class HelloWorldTest extends TestCase{
public HelloWorldTest(String name){
super(name);
}
public static void main(String args[]){
junit.textui.TestRunner.run(HelloWorldTest.class);
}
public void testSayHello() {
HelloWorld world = new HelloWorld();
assertEquals("Hello ZhangKexin!", world.sayHello());
}
}
8.在HelloWorldTest.java上点右键-->Run As-->JUnit Test,观察测试是否能通过(以下进度条为绿色是通过测试的情况)
【Task2】
Install Maven and Build the “HelloWorld” Project:
Create the directories as “Convention Over Configuration.
Use “compile, test, package” to build the project.
1.在http://maven.apache.org/download.cgi下载Mavon
2.将apache-maven-3.3.1-bin.zip解压缩到D盘
3.配置环境变量
(1)添加MAVEN_HOME,值定义为D:\apache-maven-3.3.1(解压缩的路径)
(2)在path后添加;%MAVEN_HOME%\bin;
4.在控制台中运行mvn -v,检查是否配置成功,成功如下图
4.在eclipse中新建File-->new-->other-->Maven-->Maven Project
5.分别给项目、包、文件命名,生成项目如下
6.在App.java中,编写如下代码,其中创建了一种方法sayHello()
package Maven1.Maven01; /** * Hello world! * */ public class App { public String sayHello(){ return "Hello ZhangKexin!"; } public static void main( String[] args ) { App world = new App(); System.out.println(world.sayHello()); } }
7.在AppTest.java中,编写如下代码,其中引入了assertEquals()函数
package Maven1.Maven01; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue( true ); } public void testSayHello(){ App app = new App(); String result = app.sayHello(); assertEquals("Hello ZhangKexin!",result); } }
8.在AppTest.java上点右键-->Run As-->JUnit Test,观察测试是否能通过(以下进度条为绿色是通过测试的情况)