基本使用步骤:(默认使用maven创建项目)
- pom.xml中引入依赖:
<!--单元测试的依赖--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>
- 示例编写一个java程序:
publicclassTest{publicvoidtest(){System.out.println("JUnit单元测试");}}
- 给方法加注解:@Test
publicclassTest{@Testpublicvoidtest(){System.out.println("JUnit单元测试");}}
- 点击测试运行:
查看运行结果:
依赖特点:
- 简单使用,方法上加上注解@Test即可使用
- 不需要使用main方法就能执行方法。
缺点:
- 只适用于方法测试。
So:
- 以后运行方法,可以直接使用junit依赖的@Test注解进行。
The End!!创作不易,欢迎点赞/评论!!欢迎关注个人公众号