import junit.framework.TestCase;
public class GameTest extends TestCase {
public static void main(String[] args) {
}
public void testPrintStr() {
Player player=new Player();
player.setLevelNo(5);
Game game=new Game(player);
assertEquals("详细信息",6,game.printStr().length());
}
public void test(){
Player player=new Player();
player.setLevelNo(5);
Game game=new Game(player);
assertFalse(game.wode1());
}
}这是我新建的测试类,当我导入junit.jar后使用javac编译通过了,但是使用java GameTest后,提示无法找到main 方法,我想知道在JDK环境下该如何测试呢?
一、安装JUnit
1.下载 www.junit.org
2.我下载的是4.11 要多下个 hamcrest-core-1.3.jar二、javac编译
1.没有包的情况。
a. cd 到java文件的目录
b. javac -cp D:\JUnit4.11\junit-4.11.jar FooTest.java
2. 有包的情况。
a. cd 到包层 比如有包 com.test在src下 则cd到src
b. javac -cp D:\JUnit4.11\junit-4.11.jar -d .\ com\test\FooTest.java 三、java运行
1.没有包的情况。
a. cd 到class文件的目录
b. java -cp .;D:\JUnit4.11\junit-4.11.jar;D:\JUnit4.11\hamcrest-core-1.3.jar FooTest
2. 有包的情况。
a. cd 到包层 比如有包 com.test在src下 则cd到src
b. java -cp .;D:\JUnit4.11\junit-4.11.jar;D:\JUnit4.11\hamcrest-core-1.3.jar com.test.FooTest
注意别忘了.; 不然会提示 Could not find class:FooTestP.S. FooTest的代码用于实验javac和java
import static org.junit.Assert.assertEquals;import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;/**
* Tests for {@link Foo}.
*
* @author [email protected] (John Doe)
*/
@RunWith(JUnit4.class)
public class FooTest { @Test
public void thisAlwaysPasses() {
} @Test
@Ignore
public void thisIsIgnored() {
}
}