junit的引入
通过IntelliJ IDEA创建maven项目;引入pom依赖;
注意点:
一定要将测试文件放在一起,比如test文件夹下,并且通过ide将该目录标识为Test Sources Root,不然在其他java目录建的测试代码不能引入junit,其只能在Test Sources Root里才能使用。
junit的使用
- @Test的使用:- a:(expected=XXException.class): 如果程序的异常和XXException.class一样,测试通过 1234567891011121314151617181920public class WorldTest {(expected = MException.class)public void testSay() throws Exception {throw new HException("ja;fja;fj;a");}static class HException extends Exception {HException(String msg) {super(msg);}}static class MException extends Exception {MException(String msg) {super(msg);}}}- b:(timeout = 毫秒数):如果程序运行时间小于该毫秒数,则测试通过 1234(timeout = 10)public void testHello(){// do something}
