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());

    }

}
相关推荐
IT_102411 分钟前
Spring Boot项目开发实战销售管理系统——系统设计!
大数据·spring boot·后端
Fireworkitte41 分钟前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a1530030831643 分钟前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT1 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
ai小鬼头1 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
Touper.1 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超1 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice2 小时前
对象的finalization机制Test
java·开发语言·jvm
一只叫煤球的猫2 小时前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈
一只鹿鹿鹿2 小时前
信息化项目验收,软件工程评审和检查表单
大数据·人工智能·后端·智慧城市·软件工程