文章目录
- 前言
- 环境
- 正文
- 小结
- 参考文献
前言
如题,本文给出java编写junit4 test的代码实例。
环境
- maven,jdk8
- ubuntu
正文
1)在pom.xml加入依赖:
<!-- junit test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
2)在src/test/java
文件夹下新建对应源码的package,然后在package下新建对应被测文件的测试类:
如:被测java文件为:Aaa.java
那么新建的测试类就是:AaaTest.java (这个是junit的常用命名方式)
编写测试用例:
import static org.junit.Assert.assertTrue;
import java.io.File;
import org.junit.Before;
import org.junit.Test;
/**
* @author apr
* Aug 3, 2021
*/
public class AaaTest {
private Aaa aaa;
@Before
public void init() {
aaa= new Aaa();
String filePath = "xxx";
aaa.setPath(new File(filePath));
}
@Test
public void testGetXxx() {
aaa.getXxx();
assertTrue(true);
}
}
小结
创作结束时间:
参考文献
主要参考:
- Unit Testing with JUnit 4 - Tutorial https://www.vogella.com/tutorials/JUnit4/article.html
- https://mvnrepository.com/artifact/junit/junit/4.13.1/usages
次要参考:
- How to unit test with JUnit 4 (Junit 4 tutorial with examples) https://javacodehouse.com/blog/junit-tutorial/
其他参考:
- What is the default scope of a method in Java? https://stackoverflow.com/questions/714791/what-is-the-default-scope-of-a-method-in-java