头歌-Junit实训入门篇

第1关:第一个Junit测试程序

java 复制代码
package step1;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import step1.JunitSub;

public class JunitSubTest {
    // 引入JunitSub对象
    JunitSub js = new JunitSub();

    /*
    请在下面的Begin/End内写一个测试函数,
    来验证JunitSub中的sub函数编写是否正确
    */
    /***********************Begin**************************/
    @Test
    public void testSub() {
        int result = js.sub(5, 2);
        int expected = 3;
        assertEquals(expected, result);
    }
    /************************End***************************/
}

第2关:Junit注解

java 复制代码
package step2;
 
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
 
public class JunitAnnotation {
    /*
     *以下Junit测试程序的输出结果为:
     *in before class
     *in before
     *in test
     *in after
     *in after class
     *请修改下面Begin/End内各个测试函数的注解,使输出结果逆序
     */
     /***********************Begin**************************/
 
 
 
   //execute before class
   @BeforeClass
   public static void afterClass() {
      System.out.println("in after class");
   }
 
   //execute after test
   
   @Before
   public void after() {
      System.out.println("in after");
   }
 
   //execute before test
   @After
   public void before() {
      System.out.println("in before");
   }
 
   //execute after class
   @AfterClass
   public static void beforeClass() {
      System.out.println("in before class");
   }
 
   //test case
   @Test
   public void test() {
      System.out.println("in test");
   }
 
   //execute before class
    /************************End***************************/
}

第3关:Junit断言

java 复制代码
package step3;

import static org.junit.Assert.*;
import org.junit.Test;

public class AssertionsTest {
    String obj1 = "junit";
    String obj2 = "junit";
    String obj3 = "test";
    String obj4 = "test";
    String obj5 = null;
    int var1 = 1;
    int var2 = 2;
    int[] arithmetic1 = {1, 2, 3};
    int[] arithmetic2 = {1, 2, 3};

    @Test
    public void test() {
        /***********************Begin**************************/

        assertEquals("obj1和obj2应该相等", obj1, obj2);

        assertEquals("obj3和obj4应该相等", obj3, obj4);

        assertNotEquals("obj1和obj3应该不相等", obj1, obj3);

        assertNotNull("obj1不应该为null", obj1);

        assertNull("obj5应该为null", obj5);

        assertEquals("var1应该等于1", 1, var1);

        assertNotEquals("var2应该不等于1", 1, var2);

        assertArrayEquals("arithmetic1和arithmetic2应该相等", arithmetic1, arithmetic2);


        /************************End***************************/
    }
}

第4关:Junit时间测试

java 复制代码
package step4;

import org.junit.Test;

public class TestTimeOut {
    // 请在下面的Begin/End内补全test()超时测试函数
    /***********************Begin**************************/
    @Test(timeout = 1000) 
    public void test() {
       
        while (true) {
         
        }
    }
    /************************End***************************/
}
相关推荐
金銀銅鐵2 天前
浅解 JUnit 4 第十二篇:如何生成 @Before 注解的替代品?(上)
junit·单元测试
金銀銅鐵6 天前
浅解 JUnit 4 第十一篇:@Before 注解和 @After 注解如何发挥作用?
junit·单元测试
金銀銅鐵7 天前
浅解 JUnit 4 第十篇:方法上的 @Ignore 注解
junit·单元测试
金銀銅鐵11 天前
浅解 JUnit 4 第九篇:JUnitCore (下)
junit·单元测试
钟智强11 天前
CVE-2025-49844高危预警:Redis Lua脚本引擎UAF漏洞深度剖析与POC实战
数据库·redis·web安全·junit·lua
金銀銅鐵12 天前
浅解 JUnit 4 第八篇:JUnitCore (上)
junit·单元测试
百锦再14 天前
Java中的日期时间API详解:从Date、Calendar到现代时间体系
java·开发语言·spring boot·struts·spring cloud·junit·kafka
金銀銅鐵15 天前
浅解 Junit 4 第七篇:AllDefaultPossibilitiesBuilder
java·junit·单元测试
金銀銅鐵16 天前
浅解 Junit 4 第六篇:AnnotatedBuilder 和 RunnerBuilder
后端·junit·单元测试
金銀銅鐵17 天前
浅解 Junit 4 第五篇:IgnoredBuilder 和 RunnerBuilder
junit·单元测试