Beruflich Dokumente
Kultur Dokumente
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.
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
Run
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