EE308 Lab11

crisitianoyyb 2021-12-14 15:33:03
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ
The Goal of This AssignmentSoftware Testing
Leader's MU STU ID and Name19103379_831902122
Teammate 1's MU STU ID and Name19104065_831902128
Teammate 2's MU STU ID and Name19103913_831902127
Teammate 3's MU STU ID and Name19103964_831902125
Teammate 4's MU STU ID and Name19104090_831902111

We test some functions of our back-end part of the program.

1. Junit assert test

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bcslr.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.bcslr.entity.User;
import org.springframework.test.context.junit4.SpringRunner;
// import org.junit.rules.ExpectedException;
// import org.springframework.util.Assert;
// import static org.junit.Assert.*;


import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j


class BcslrApplicationTests extends ServiceImpl<UserMapper, User> {
    @Test
    public void test() {
        // add assert test code between Begin and End, no other change allowed
        /***********************Begin**************************/
        Assert.assertEquals("var1" , var2, var1); //
        // for example
        /************************End***************************/
    }
}

img

img

The Junit assertEquals function can check whether two values are same, and give the corresponding output like the error information.

2. Junit time test


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bcslr.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.bcslr.entity.User;
import org.springframework.test.context.junit4.SpringRunner;
// import org.junit.rules.ExpectedException;
// import org.springframework.util.Assert;
// import static org.junit.Assert.*;


import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j


class BcslrApplicationTests extends ServiceImpl<UserMapper, User> {
 /*
    test timeout function
    */
    @Test()
    public void testTimeout(){
        log.info("yybzjbs");
        while(true){
            log.info("yybzjbs");
        }
    }
}

img

The Junit time test function are capable to prevent the function run for overlong time, it can prevent them to run when the function reaches the timeout function limitation and output the wrong information.

3. Junit parameterized test

package com.example.bcslr;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.example.bcslr.Calculate;
import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;

/**
 * JUnit4 parameterized test
 */
@RunWith(Parameterized.class)
public class ParameterTest {

    private final int input11;
    private final int input22;
    private final int expected;

    public ParameterTest(int input11, int input22, int expected) {
        this.input11 = input11;
        this.input22 = input22;
        this.expected = expected;
    }

    @Parameterized.Parameters()
    public static Collection<Object> prepareData() {
        return Arrays.asList(new Object[][]{
                {3, 1, 2}, {5, 2, 3}//, {3,4,5}, {6,7,8}
        });
    }

    @Test
    public void testSub() {
        Calculator cal = new Calculator();
        assertEquals(cal.sub(input11, input22), expected);
    }
    // Calculator.java Junit Parameterized Test
    /**
     * Mathematical Calculation  subtract
     */
    public class Calculator {
        public int sub(int a, int b) {
            return a - b;
        }
    }

}

img

Junit parameterized test function makes it possible to run tests with multiple kinds of parameters, in we can test multiple parameters.

4. Junit Exception Test

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bcslr.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.bcslr.entity.User;
import org.springframework.test.context.junit4.SpringRunner;
// import org.junit.rules.ExpectedException;
// import org.springframework.util.Assert;
// import static org.junit.Assert.*;


import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j


class BcslrApplicationTests extends ServiceImpl<UserMapper, User> {
    @Test
    void testnegid() {
        User user = new User();
        user.setName("王宁");
        user.setAge(-1);//怎么改id??
    }

}

img

Junit Exception Test will stick with the functions in the original places and throw exceptions as usual.

5. Junit Suite Test

package com.example.bcslr;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;


@RunWith(Suite.class)
@Suite.SuiteClasses({TestDemo2.class,TestDemo.class})
public class TestSuite {

    @org.junit.Test(timeout = 1000)
    public void test2() {
        // log.info("yybzjbs");

        //log.info("yybzjbs");
        System.out.println(1);

    }
}


img

The two functions in the TestDemo2.java file are able to run fluently by using the junit.

img

The two functions in the TestDemo1.java file are able to run fluently by using the junit.

img

Junit Suite Test is capable to test the subset functions and files from the java files that include by Suite functions. In this way, it test all the functions and cases from TestDemo.java file and TestDemo2.java file. And, we can see the four cases all pass fluently like we test them individually, and we can see the running performance individually.

...全文
198 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

183

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程 教学
软件工程 高校
社区管理员
  • 单步调试
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧