Java Demo - JUnit :Unit Test(Assert Methods)

If a developer wants to test methods or processing logic, we can use the JUnit framework to test our backend code. This content uses the Java language to create a simple demo to show how to use the assert parameter to complete unit tests.

1.For Mave project - add dependency

add JUnit dependency:if you are using Maven,you can add the dependency to your pom.xml file

XML 复制代码
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.8.2</version>
    <scope>test</scope>
</dependency>

2.Creat a Java Demo to be test

java 复制代码
public class UnitTestDemo  {

    public int add(int i,int j){
        return i+j;
    }

    public boolean compareValue(int i, int j){
        return i > j;
    }

    public int addAndThowException(int i, int j){
        if(i!=0 || j!=0) {
            return i+j;
        }else {
            throw new RuntimeException();
        }
    }

    public void displayMsg(int i){
        if(i==1) {
            System.out.println("When i euqals 1 display this sentence");
        }
    }
}

3.Creat a JUnit test class

@BeforeEach: executed before each test method

@AfterEach: executed after each test method

@BeforeAll: executed once before all test methods

@AfterAll: executed once after all test methods

@Test: is used to mark test methods

java 复制代码
public class UnitTestDemoTests {

    private static UnitTestDemo unitTestDemo;
    private static final ByteArrayOutputStream outContent = new ByteArrayOutputStream();

    @BeforeAll
    public static void createCommand() {
        unitTestDemo = new UnitTestDemo();
        System.setOut(new PrintStream(outContent));
    }

    @Test
    public void testNull() {
        Object obj = new Object();
        assertNotNull(obj);
        obj = null;
        assertNull(obj);
    }

    @Test
    public void testArrayEquals() {
        int[] expected = {1, 2, 3};
        int[] actual = {1, 2, 3};
        assertArrayEquals(expected,actual);

        List<Integer> expectedList = Arrays.stream(expected).boxed().collect(Collectors.toList());
        List<Integer> actualList = Arrays.stream(actual).boxed().collect(Collectors.toList());
        assertIterableEquals(expectedList,actualList);
    }

    @Test
    public void add_testEquals() {
        int result = unitTestDemo.add(2,3);
        assertEquals(5,result);
    }

    @Test
    public void compareValue_testResult() {
        boolean equalsTrue = unitTestDemo.compareValue(3,2);
        assertTrue(equalsTrue);
        boolean equalsFalse = unitTestDemo.compareValue(2,3);
        assertFalse(equalsFalse);
    }


    @Test
    public void add_testNotEquals() {
        int result = unitTestDemo.add(2,3);
        assertNotEquals(4,result,"2 + 3 不应等于 4");
    }

    @Test
    public void addAndThowException_testEquals() {
        int result = unitTestDemo.addAndThowException(2,3);
        Assertions.assertDoesNotThrow(() -> result);
        assertEquals(5,result,"2 + 3 应等于 5");

    }

    @Test
    public void addAndThowException_testThowException() {
        int i=0,j=0;
        assertThrows(RuntimeException.class,() -> unitTestDemo.addAndThowException(i,j));
    }

    @Test
    public void displayMsg_test() {
        Assertions.assertDoesNotThrow(() -> unitTestDemo.displayMsg(1));
        assertTrue(
                outContent.toString()
                        .endsWith("When i euqals 1 display this sentence" + System.lineSeparator())
        );
        assertEquals("When i euqals 1 display this sentence"
                +System.lineSeparator(), outContent.toString());

    }

}
相关推荐
ZPC82101 天前
docker 入门2
java·linux·数据库
RunsenLIu1 天前
基于 Spring Boot 3 与 Vue 3 的家校互动平台
vue.js·spring boot·后端
codeejun1 天前
每日一Go-25、Go语言进阶:深入并发模式1
开发语言·后端·golang
吹晚风吧1 天前
实现一个mybatis插件,方便在开发中清楚的看出sql的执行及执行耗时
java·sql·mybatis
杜子不疼.1 天前
SpringBoot + Vue 前后端分离项目实战:权限 + 工作流 + 报表
vue.js·spring boot·后端
让我上个超影吧1 天前
【力扣34】在排序数组中查找元素的第一个和最后一个位置
java·数据结构·算法·leetcode
逍遥德1 天前
Maven教程.04-如何阅读Maven项目
java·maven
xiaoliuliu123451 天前
treeNMS-1.7.5部署步骤详解(附Java环境准备与数据库配置)
java·开发语言·数据库
没有bug.的程序员1 天前
订单系统重构史诗:从单体巨兽到微服务矩阵的演进、数据一致性内核与分布式事务
java·微服务·矩阵·重构·分布式事务·数据一致性·订单系统
m0_738120721 天前
应急响应——Solar月赛emergency靶场溯源过程(内含靶机下载以及流量分析)
java·开发语言·网络·redis·web安全·系统安全