1. 创建Maven项目
  2. 在pom.xml中添加引用包Junit
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
  1. maven buildMaven下Junit的使用

  2. 写代码 (以判断是否为闰年为例)
    `public class DateJunit {
    public static void main(String[] args) {

    }
    static boolean isLeapYear(int year) {

    if(year < 0 || year >10000) {
    	return false;
    }
    if(year % 4 == 0) {
    	if(year % 400 != 0 && year % 100 == 0) {
    		return false;
    	}else {
    		return true;
    	}
    }else {
    	return false;
    }
    

    }

} `

  1. 生成测试类 test目录 ->new -> Junit Test Cast
import static org.junit.Assert.*;

import org.junit.Test;

public class TestisLeapYear {

	@Test
	public void test() {
		//fail("Not yet implemented");
		//-100, 1000, 20000, 2020, 2019, 2000, 1900.
		assertEquals(false, DateJunit.isLeapYear(-100));
		assertEquals(false, DateJunit.isLeapYear(1000));
		assertEquals(false, DateJunit.isLeapYear(20000));
		assertEquals(true, DateJunit.isLeapYear(2020));
		assertEquals(false, DateJunit.isLeapYear(2019));
		assertEquals(true, DateJunit.isLeapYear(2000));
		assertEquals(false, DateJunit.isLeapYear(1900));
	}

}

  1. 运行 运行可以单个测试文件运行也可以Maven集体运行
    Maven下Junit的使用
    Maven下Junit的使用

相关文章:

  • 2021-07-23
  • 2021-06-19
  • 2021-06-30
  • 2022-01-01
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
猜你喜欢
  • 2021-04-30
  • 2021-06-02
  • 2022-12-23
  • 2021-10-19
  • 2022-12-23
  • 2021-12-17
相关资源
相似解决方案