P. 1
JUnit Tutorial

JUnit Tutorial

|Views: 60|Likes:
Published by Balaji Marimuthu

More info:

Published by: Balaji Marimuthu on Jul 04, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

07/04/2011

pdf

text

original

JUnit Tutorial

Hong Qing Yu Nov 2005

JUnit Tutorial 
The

testing problems  The framework of JUnit  A case study  JUnit tool  Practices

2

The Testing Problems Should write Do programmers few Why? I am so busy It is difficult 3 .

or even better. then a test that should run.The Testing Problems Programmers need such kind of tool: ³Writing a few lines of code. to write a test that won't run. then write the code that will make it run.´ JUnit is that kind of tool! 4 .

JUnit Tutorial  The testing problems  The framework of JUnit  A case study  JUnit tool  Practices 5 .

The Framework of JUnit 6 .

JUnit Tutorial  The testing problems  The framework of JUnit  A case study  JUnit tool  Practices 7 .

String currency) { fAmount= amount.A Case Study class Money { private int fAmount.amount(). } public int amount() { return fAmount. fCurrency= currency. } } public String currency() { return fCurrency. } } 8 . currency()). private String fCurrency. public Money add(Money m) { return new Money(amount()+m. public Money(int amount.

// (2) // (3) Assert.assertTrue(expected. // (1) Money m14CHF= new Money(14.How to Write A TestCase public class MoneyTest extends TestCase { //« public void testSimpleAdd() { Money m12CHF= new Money(12. "CHF"). Money expected= new Money(26. "CHF"). Money result= m12CHF. (2) Exercises the objects in the fixture. } } (1) Creates the objects we will interact with during the test.add(m14CHF). (3) Verifies the result 9 . "CHF"). This testing context is commonly referred to as a test's fixture. All we need for the testSimpleAdd test are some Money objects.equals(result)).

actual) assertTrue(condition) assertTrue(message. delta) assertEquals(message. actual) Assert(Not)Same(message. expected. actual. actual) assertEquals(message. actual. expected. delta) assertFalse(condition) assertFalse(message. object) Assert(Not)Same(expected. actual) assertEquals(expected. expected. condition) Assert(Not)Null(object) Assert(Not)Null(message.Assert             assertEquals(expected. condition) 10 .

Structure  setUp() Storing the fixture's objects in instance variables of your TestCase subclass and initialize them by overriding the setUp method tearDown() Releasing the fixture¶s run() Defining how to run an individual test case. Defining how to run a test suite. testCase()    11 .

"CHF"). } TestCase test= new MoneyTest("simple add") { public void runTest() { testSimpleAdd(). Money result= f12CHF.Structure of Writing A Test public class MoneyTest extends TestCase { private Money f12CHF. } } } 12 . "CHF"). Assert. } public void testSimpleAdd() { Money expected= new Money(26.add(f14CHF). "CHF"). private Money f14CHF. protected void setUp() { f12CHF= new Money(12. f14CHF= new Money(14.assertTrue(expected.equals(result)).

«. « 0 Bigger than 0 such as 1. -100. 100« 13 . «. 2. -2.Design Test Cases   The real world scenarios The number boundaries Smaller than 0 such as ±1.

amount(). } } public String currency() { return fCurrency. } } 14 .amount()<=0) throw exception.The Feedback to Code class Money { private int fAmount. fCurrency= currency. public Money(int amount. private String fCurrency. return new Money(amount()+m. } public int amount() { return fAmount. String currency) { fAmount= amount. currency()). public Money add(Money m) { if (m.

suite.addTest(new MoneyTest("testSimpleAdd")). } 15 .Test Case and Test Suite TestCase test= new MoneyTest("simple add") { public void runTest() { testSimpleAdd(). } } public static Test suite() { TestSuite suite= new TestSuite(). return suite. suite.addTest(new MoneyTest("testEquals")).

0 there is an even simpler dynamic way. } } TestCase test= new MoneyTest("testSimpleAdd").class).Static and Dynamic Run  1. JUnit supports two ways of running single tests: static dynamic TestCase test= new MoneyTest("simple add") { public void runTest() { testSimpleAdd(). You only pass the class with the tests to a TestSuite and it extracts the test methods automatically. 2. public static Test suite() { return new TestSuite(MoneyTest.  Since JUnit 2. } 16 .

JUnit Tutorial  The testing problems  The framework of JUnit  A case study  JUnit tool  Practices 17 .

JUnit 18 .

JUnit for Eclipse 19 .

net/junit/ 2.TestRunner junit.jar 3.sourceforge. Installation  unzip the junit. Download the latest version of JUnit from http://download. JUnit plug-in for Eclipse 20 . All the tests should pass OK.AllTests for the graphical TestRunner type: java junit.samples.samples.AllTests for the Swing based graphical TestRunner type: java junit.Start to Use it 1.INSTALL_DIR\junit3\junit.awtui.TestRunner junit.zip file  add junit. Testing Test the installation by using either the batch or the graphical TestRunner tool to run the tests that come with this release.textui.samples.jar into the extension directory of your JDK installation. If you do so the test class on the files system will not be found.jar but in the installation directory directly.AllTests Notice: The tests are not contained in the junit.TestRunner junit.    for the batch TestRunner type: java junit. For example: set classpath=%classpath%.jar to the CLASSPATH. Therefore make sure that the installation directory is on the class path Important: Don't install the junit.swingui.

org/viewcvs/index.Eclipse plug-in http://dev.eclips e.eclipse.0.eclipse.pde.zip 21 .jdt.0.junit/org.cgi/jdt-uihome/plugins/org.junit_3.

 The testing problems  The framework of JUnit  A case study  JUnit tool  Practices 22 .

Practices BankAccount case study 23 .

org/index.net/javadoc/junit/frame work/ 24 .cs.More Reading      http://www.cs.htm http://open.umanitoba.bham.edu/se/tutorials/junit/ http://www.pdf http://supportweb.ncsu.junit.ca/~eclipse/10JUnit.sourceforge.ac.p df http://junit.uk/documentatio n/tutorials/docsystem/build/tutorials/junit/junit.

cs.Contact information hqy1@le.uk Today¶s slides can be find at http://www.ac.ac.uk/people/hqy1/JUnit% 20Tutorial.le.ppt 25 .

Thanks And Questions 26 .

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->