EE308FZ LAB 11 Junit Test

Hester1028 2021-12-15 15:27:44

The Link Your Class

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

The Link of Requirement of This Assignment

LAB 11 Software Testing-CSDN社区

The Aim of This Assignment

Software Testing

MU STU ID and FZU STU ID

19105428_831902220

Preparation

Learn related knowledge about Junit Test for Java and Unittest for python. You can finish the following assignment or test Your Project.

Task

  1. Here are Junit Test task and Unittest task for choose. If you are good at Java , we suggest you choose the first one, if you are good at Python, then, the latter one.
  2. Follow the requests and tips, complete the test task .

Requirements

  1. You should be familiar with Junit Test or Unittest and the usage of these tools.Such as ‘assert, suite, timeout’, check problems for detail.
  2. Fix the missing codes according to requirement for every problem.

Tips:

@Parameters:Add this annotation to every method if it provide data. By the way, these methods must be static, return a Collection and receive no parameter. 

P.S.: You must assign value for every field in the class no matter used or unused!

In Junit, you can use @RunWith and @parameter to pass parameters.

@RunWith:When a class annotated by @RunWith or when a class extends a base class which annotated by @RunWith,then Junit will run test through a runner pointed by the annotation.

The following is the code I use to test:

Junit Test

  • Junit assert

  • Related Knowledge: you can check the expected result and the real result of the method you test with the help of org.junit.Assert.

 

 

1.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(var1 < var2);
        assertFalse(obj4==obj5);
        assertEquals(obj1,obj2);
        assertSame(obj1,obj2);
        assertNotSame(obj1,obj3);
        assertArrayEquals(arithmetic1,arithmetic2);
        assertNotNull(obj1);
        assertNull(obj5);
        //assertTrue(var1 < var2); for example
	    /************************End***************************/
        }
}

2. 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()
     {
        int i = 0;
    	while(true)
        {
        	break;
        }
    }
    /************************End***************************/
}

3.Junit parameterized test

Related knowledge:Junit parameterized test allows you test the same test with  different parameters. Learning Junit parameterized test with five steps bellow.

  1. Annotate test class with @RunWith(Parameterized.class).
  2. Build a static method annotated by @Parameters, which retrun a set or an array of Objects as test data.
  3. Build a public construct method, which receives a parameter equals with test data.
  4. Build instance variable for every colum of test data.
  5. Build your test case with the instance as test data source.

In Junit, you can use @RunWith and @parameter to pass parameters. @RunWith:When a class annotated by @RunWith or when a class extends a base class which annotated by @RunWith,then Junit will run test through a runner pointed by the annotation.@Parameters:Add this annotation to every method if it provide data. By the way, these methods must be static, return a Collection and receive no parameter. 

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[][] ages={{2,2,0},{0,1,-1},{0,1,-1},{3,1,2}};
		return Arrays.asList(ages);
        /**********************************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;  
    }  
}

4.Junit Exception Test

Related knowledge:you can check codes if throw expected exception or not by using ‘expected’ attribute in @Test meta data. value of the ‘expected attribute’ is a kind of Exception, if codes throw the expected exception, then test successfully, otherwise, failed.

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;
    }
}

5.Junit Suite Test

Related knowledge:Suite Test means test a couple of test cases together. Precisely speaking, Using @RunWith and @Suite.

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)
@Suite.SuiteClasses({ CalculateTest.class, CarTest.class})
public class SuiteTest {
    @Test
    public void testPrint() {
        System.out.println("The result of CalculateTest");
assertEquals(24, result);
System.out.println("The result of CarTest");
assertEquals(4, result);
    }
}
 
//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;
    }
}

 

 

 

 

 

 

 

 

...全文
173 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文提出了一种基于混沌系统与DNA编码的复合型彩色图像加密解密方案,旨在解决传统图像加密算法安全性不足及抗干扰能力弱的问题。方案通过混沌系统的伪随机序列对图像像素进行全局置乱,并结合DNA碱基编码的多维扩散特性实现像素值的深层混淆,构建了“置乱-扩散”双重保护机制。研究不仅系统验证了算法在密钥空间、密钥敏感性、信息熵等方面的安全性能,还重点开展了多场景、多强度的抗噪声与抗裁剪性能分析,证实了其在高斯噪声、椒盐噪声干扰及不同位置、面积裁剪攻击下的卓越鲁棒性,弥补了现有研究在实用性方面的不足。; 适合人群:具备一定信息安全、图像处理或密码学基础,从事科研或工程开发的研究生、教师及技术人员。; 使用场景及目标:①为彩色图像在网络传输、医疗影像、军事通信等高安全需求场景下的保密存储与传输提供高鲁棒性加密方案;②为研究复合型图像加密算法的设计与性能评估提供系统性参考; 阅读建议:读者应重点关注混沌与DNA编码的融合机制、加密流程设计以及抗干扰性能的测试方法,建议结合文中提到的仿真结果深入理解算法的实用性与优化方向,并可进一步探索其在视频加密或多模态数据保护中的扩展应用。
内容概要:WiFiSupply斯普莱WFS7000BDAX是一款高性能、电信工业级双射频双频分体式无线基站,支持2.4G和5.8G频段的WiFi6(802.11ax)标准,采用4T4R MIMO架构,双射频最大带宽达2974Mbps,点对点实际吞吐量超过1500Mbps。设备具备高发射功率(单射频28dBm)、超强抗干扰能力、通信延迟≤1ms、支持OFDMA、FastRoaming快速漫游、LD长距离优化协议等核心技术,配合高增益天线可实现3km覆盖或150公里点对点桥接。产品支持宽电压POE供电(12-30V,可选48V),具备IP68防护等级和-45~85℃宽温工作能力,通过多项国家级认证,适用于恶劣环境下的稳定运行。支持胖瘦一体模式、云管理平台、API对接、SNMP、SSH等丰富管理方式,提供多重加密与安全防护机制,适用于工业4.0、智慧物流、智能驾培、AGV小车、车载船载视频传输等复杂场景。; 适合人群:从事工业无线通信、网络工程、物联网系统集成的技术人员及项目决策者,具备一定网络基础知识和现场部署经验的工程师。; 使用场景及目标:①用于远距离无线桥接与大面积无线覆盖,如森林防火监控、智慧交通信号回传;②满足高密度、高移动性终端接入需求,如AMR/AGV小车组网、无人机遥控;③在强电磁干扰或极端气候环境下构建高可靠无线网络,保障视频传输、PLC控制等关键业务连续性;④通过云平台或本地服务器实现集中运维管理和安全认证。; 阅读建议:此文档为产品技术规格说明书,重点在于了解设备性能边界与适用场景,建议结合实际项目需求对照参数选型,并关注其私有协议、安全隧道、抗干扰设计等差异化功能的实际部署配置方法。

183

社区成员

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

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