You are on page 1of 31

NGN NG LP TRNH

JAVA
1
Ni dung
Bin, t kho,
kiu d liu
Lp trnh hng
i tng
Biu thc, cc
cu trc iu
khin
D liu kiu
mng
C bn v ngn ng lp trnh Java
Thit k lp
nng cao
Thit k lp
Cc kha cnh nng cao ca lp trnh hng i tng
X l ngoi l
Java Collection
Framework
Generics
Network
Programming
2
Multithread&
Concurrency
Database
Programming
Send mail Testing
TESTING
3
Ni dung
1. Gii thiu v testing
2. Junit Unit Testing
3. Example
4
1. Gii thiu v testing
Testing (kim th) l mt qu trnh kim tra chc
nng ca pm, xem pm c thc hin theo ng
yu cu hay khng

5
Phn loi testing
Kim th n v (Unit Test):
kim th cc lp hoc component ring l (c lp)
Kim th tch hp (Integration Test):
kim th mt nhm cc lp hoc components
Kim th chc nng (Functional Test) :
kim th trn mt h thng tch hp hon chnh
kim th trn giao din ngi dng
Kim th s suy thoi (Regression Test):
kim th m bo mt s thay i (nng cp, v li, ) khng
lm hng h thng hoc gy ra thm li mi.
6
Unit testing
Kim th tng lp/component mt cch c lp,
m bo rng tng lp/component chy theo
ng c t.
Unit testing rt cn thit cho vic m bo cht
lng phn mm
Trc khi c khi nim Unit testing
LTV s dng cc cu lnh write (hoc file trace) in ra
gi tr cc biu thc
Qu nhiu cu lnh in s gy kh khn trong vic phn tch kt
qu
Assertion : kim tra cc gi nh v logic ca chng
trnh (pre- & post-conditions, invariants)
2 loi kim th n v : manual v automated
7
Manual testing vs Automated testing
Thc hin cc test case
mt cch th cng, ko c
s tr gip ca cng c
Tn thi gian, nhm chn
Tn ti nguyn con ngi
Km tin cy
Khng c lp trnh c
=> ko to c cc test
case phc tp
S dng cng c chy
cc test case
Thc hin test case t
ng => nhanh hn
Yu cu t ti nguyn con
ngi hn
Tin cy hn
Lp trnh c => to
c cc test case phc
tp
8
xUnit Unit Testing Framework
xUnit : mt nhm cc unit testing frameworks chia s
chung mt kin trc phn mm
Java => JUnit
.NET => Nunit
C++ => CppUnit
PHP => PHPUnit
v.v
Mt s thnh phn c bn ca xUnit:
Test case/ Test suites
Test fixtures: thit lp cc phng thc set up v clean up cho
mi ln triu gi phng thc v lp
Test runner: ci t cch thc cc test case c thc thi
Test result formatter: nh dng li kt qu test
Assertions: assertion methods for all primitive types and Objects and
arrays


9
History of JUnit
Kent Beck pht trin xUnit automated test tool cho
Smalltalk vo gia thp k 90s
Beck v Gamma (tc gi ca design patterns
Gang of Four) pht trin JUnit trn chuyn bay t
Zurich ti Washington, D.C.
Junit tr thnh cng c test chun cho Java
(Junit.org)
Junit test generators tr thnh thnh phn ko th
thiu ca nhiu Java IDEs (Eclipse, BlueJ,
Jbuilder, DrJava)
K t y, xUnit tools c pht trin cho nhiu
ngn ng (Perl, C++, Python, Visual Basic, C#, )
10
APIs JUnit
11
Assert class
12
Mi assertEquals method c cc tham s nh
sau: message, expected-value, actual-value

Assert methods
13
TestCase Class
14
TestResult Class
TestResult thu thp cc kt
qu ca vic thc thi mt test
case.
L mt th hin ca mu
Collecting Parameter pattern.
Test framework phn bit gia
failures v errors.
Failure c d on v kim
tra vi assertions.
Errors l cc vn ko d
on c (ngoi l nh
ArrayIndexOutOfBoundsExcep
tion).
15
TestSuite Class
TestSuite nhm mt tp cc testcase, v thc hin chng
16
Kin trc JUnit
17
TestRunner chy cc tests v tr v kt qu TestResult
Kim th mt lp bng cch k tha abstract class
TestCase
vit cc test case, cn phi bit v hiu Assert class
Ci t JUnit
Goto http://junit.org/
"Download and Install Guide"
Download the "junit.jar" and "hamcrest-core.jar".
Cu hnh
include JUnit jar-files "junit-4.##.jar" and "hamcrest-core-1.##.jar" in
your CLASSPATH.
Cu hnh Junit vi Eclipse
Create a new Java project right-click on the project Properties
Java Build Path "Libraries" tab
Add Library JUnit In "JUnit library version", choose "JUnit 4"
In "current location" use the eclipse's JUnit or your own download.
To test case (hoc test suite)
File Others Java JUnit JUnit test case (or JUnit test suite).
Chy test case (hoc test suite)
Run As JUnit Test.
18
Vit testcase
bt u s dng JUnit, to mt lp k tha ca
TestCase:

import junit.framework.TestCase;
public class TestBowl extends TestCase {
} //Test my class Bowl

Tn ca lp l quan trng nn c dng TestMyClass
hoc MyClassTest
Quy c t tn nh vy cho php TestRunner t
ng tm c cc lp test

19
Vit testcase
Khun mu cho 1 testcase:
Set up preconditions
Exercise functionality being tested
Check postconditions

Example:

20
3. V d - Calculator

21
Vit testcase
Create a new Eclipse Java project called "JUnitTest".

Create a new class called "Calculator" under "src" folder,
with the above program code.

Create a new folder called "test" for storing test scripts
Right-click on the project New Folder In folder
name, enter "test". Make "test" a source folder by right-
click on "test" Build Path Use as source folder.

Create the first test case called "AddSubTest" Right-
click on folder "test" New Other Java JUnit
JUnit Test Case New JUnit 4 test In Name, enter
"AddSubTest". Enter the following codes:

22
Vit testcase
23
Kim th vi TestRunner

24
Thm testcase
25
Vit TestSuite
26
To create a test suite under Eclipse right-click on the test folder New
other Java JUnit JUnit Test Suite In Name, enter "AllTests"
Select test cases to be included - AddSubTest and DivTest.
To run the test suite right-click on the file Run as JUnit Test. Observe
the test results produced.
Junits 4s annotations

27
Example 2
Create a Java project called "JUnitTest2"
Create a new Java class called "MyNumber", as follow:

28
Create a new source folder called "test" for storing test
scripts. Make it a source folder by right-click Build Path
Use as source folder.
Create the first test case called MyNumberTest (under
"test" folder), as follows:

29
30
31

You might also like