EE308 LAB11

这篇博客详细介绍了Java单元测试的各种用例,包括JUnit的断言测试、时间限制测试、参数化测试、异常测试以及测试套件的实现。通过具体的代码示例展示了如何使用JUnit进行测试,包括测试用例的编写、异常处理、超时设置以及参数化测试数据的传入。此外,还展示了测试结果,强调了即使只有一个测试失败,整个单元测试也会失败。
摘要由CSDN通过智能技术生成

The Link Your Class

https://bbs.csdn.net/forums/MUEE308FZ

The Link of Requirement of This Assignment

The Aim of This Assignment

Software Testing

MU STU ID and FZU STU ID

19104553_831902205

Directory

Junit assert

Junit time test

Junit parameterized test

Junit Exception Test

Junit Suite Test


Junit assert

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() {
		// add assert test code between Begin and End, no other change allowed 
		/***********************Begin**************************/
		assertTrue(arithmetic1.length == arithmetic2.length);
		assertFalse(arithmetic1.length < arithmetic2.length);
		assertEquals(arithmetic1.length, 3);
		assertNotNull(arithmetic1);
		assertSame(arithmetic1, arithmetic2);
		assertArrayEquals(arithmetic1, arithmetic2);
		/************************End***************************/
    }
}

 The result of Junit is the figure shown below:

If I change the code into this way:

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() {
		// add assert test code between Begin and End, no other change allowed 
		/***********************Begin**************************/
		assertTrue(arithmetic1.length == arithmetic2.length);
		assertFalse(arithmetic1.length < arithmetic2.length);
		assertEquals(arithmetic1.length, 3);
		assertNotNull(arithmetic1);
		//assertSame(arithmetic1, arithmetic2);
		//assertArrayEquals(arithmetic1, arithmetic2);
		/************************End***************************/
    }
}

 The result is shown below:

Therefore, it can be concluded that the unit test will not pass even if there is only one failure in the test.

Junit time test


import org.junit.Test;

public class TestTimeOut {

     // Fix timeout assert in Test function below. Test fail if running 1000ms longer
    /***********************Begin**************************/
    @Test(timeout = 1000)
    public void test() {
        while(true){}
    }
    /************************End***************************/
}

The result is shown below:

Thus, it can be concluded that this function runs 1000ms longer.

Junit parameterized test

import static org.junit.Assert.assertEquals; // static import
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import step1.Calculator;
/**
 * JUnit4 parameterized test
 */
@RunWith(Parameterized.class)
public class ParameterTest {
    private int input11;
    private int input22;
    private int expected;
    public ParameterTest(int input11, int input22, int expected){
        this.input11 = input11;
        this.input22 = input22;
        this.expected = expected;
    }
    @Parameters
    public static Collection prepareData(){
        /**
         *the type of the two-dimension array must be Object.
         *data in the two-dimension array is ready of test sub() in Calculator
         * every element in the two-dimension array should corresponds to position of parameters in construct method ParameterTest
         *let the third element equals the first subtract the second element according to parameters’ postion
         *fix missing codes under ‘Begin’ and above ‘End’,pass 4 groups of parameters to test sub method in Calculator is right or not 
         *tip:only two lines of codes 
         */
        /*********************************Begin********************************************/
        Object o1[][] = {{0, 1, -1},{3, 3, 0},{6, 5, 1},{10, 8, 2}}; 
		return Arrays.asList(o1);
        /**********************************End********************************************/
    }
    @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;  
    }  
}

The result is shown below:
 

Junit Exception Test

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import step2.Person;



public class JunitException {

    /**
*add a line of annotation in Begin/End,check the age of Person Object is legal or not.     *throw IllegalArgumentException exception
    */
    /***********************************Begin***********************************/
    @Test(expected = IllegalArgumentException.class)
  /************************************End************************************/
    public void checkage() {
    Person person = new Person();
    person.setAge(-1);
    }
}
//Person.java
public class Person {
        private String name;
        private int age;
        public String getName() {
        return name;
        }
        public void setName(String name) {
        this.name = name;
        }
        public int getAge() {
        return age;
        }
        public void setAge(int age) {
        if (age < 0 ) {
        throw new IllegalArgumentException("age is invalid");
        }
    this.age = age;
    }
}

The result is shown below:

Junit Suite Test

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import step3.Calculate;
import step3.CalculateTest;
import step3.Car;
import step3.CarTest;

/*
add two lines of annotations. Implement Suite Test of CalculateTest and CarTest
Suite Test codes must next to Class SuiteTest, no shift allowed!
*/
//**************************************************************
@RunWith(Suite.class)
@SuiteClasses({CalculateTest.class, CarTest.class})
public class SuiteTest {
}
//Calculate.java
public class Calculate {
    public int add(int a, int b) {
        return a + b;
    }
}

//CalculateTest.java
public class CalculateTest {
    Calculate calculate;
    @Before
    public void setUp() throws Exception {
        calculate = new Calculate();
    }
    @Test
    public void testAdd() {
        int result = calculate.add(12, 12);
        assertEquals(24, result);
}
}

//CarTest.java
public class CarTest {
    Car car;
    @Before
    public void setUp() throws Exception {
        car = new Car();
    }
    @Test
    public void testGetWheels() {
        int result = car.getWheels();
        assertEquals(4, result);
    }
}

//Car.java
public class Car {
    public int getWheels() {
        return 4;
    }
}

The result is shown below:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值