Sie sind auf Seite 1von 20

CSE4006: Software Engineering

Lab 11: Testing


Software Engineering Lab

Except where otherwise noted, the contents of this document are Copyright 2017 Gwanggyu Choi, Youn-geun
Ahn and Scott Uk-Jin Lee All rights reserved. Any redistribution, reproduction, transmission, or storage of part
or all of the contents in any form is prohibited without author’s expressed written permission.
Unit Test
What is Unit Test?
• The process of testing program components, such as
methods or object classes.

What is JUnit Test?


• Open source unit test framework for Java

JUnit Test Features


• Annotations to identify test methods
• Assertion functions for testing expected result
Setup
1. Download(here) BinaryCalculator.java file.
2. Create new java project and put the code in.
(* This lab uses Eclipse)
Setup

Project  Properties
Setup

Add Library… 
Setup

1. Select ‘JUnit’
2. Click ‘Next >’ button
Setup

1. Select ‘Junit 4’
2. Click ‘Finish’ button
Setup

Apply and OK
Create JUnit test cases

1. Right click a package


(You may create new package for unit test)
2. New  JUnit Test Case
Create JUnit test cases

1. Write unit test name


2. Finish
Simple Unit Test

Run

JUnit runs methods which annotated with @Test


Multiple Unit Test
Unit Test Fail
Assert Functions

Function Description
assertArrayEquals(arr1, arr2) Asserts that two arrays are equal.
assertEquals(expected,actual) Asserts that two values are equal.(a.equals(b))
assertSame(expected, actual) Asserts that two objects are same.(a == b)
assertTrue(condition) Asserts that the condition is true.
assertNotNull(object) Asserts that the object is not null.
@Test Annotation
Set timeout
• @Test(timeout = 5000)
: Test fails if test case not passed in 5sec.

Catch Exception
• @Test(expected = BlahBlahException.class)
: Test fails if test case not throws BlahBlahException.
More Annotations

Annotation Description
@BeforeClass Running when the Unit Test starts
(e.g. DB Connect, Environment setting, …)
* Must be static method
@AfterClass Running when the Unit Test ends
(e.g. release resources, disconnect DB)
* Must be static method
@Before Running before a test case starts
@After Running after a test case ends
More Annotations
Exercise 1. Annotation Flow
1. Create some unit test case methods
2. Create a method for each annotations(before, after, …)
3. Use System.out.println, show when the methods are calling.

Example
Exercise 2. Make Unit Test Cases
1. Download code from http://pasted.co/82ced221
2. Write test code for each method.
3. Check halfTest() is equal to halfText2().
4. If there exists some bug, fix it all.
Submission
Submission directory structure

[Lab11]Yourname ____ Ex1 _____ UnitTest.java


| |
| |___ result.png
|
|__ Ex2 _____ UnitTest.java
|
|___ result.png

1. Zip them into SE_Yourname.zip


2. Send frebern@hanyang.ac.kr (mail title: [Lab11]Yourname)
Due Date: 6/4(Sun) 11:59pm

Das könnte Ihnen auch gefallen