My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
JUnitQuickTutorial  
A quick JUnit tutorial for beginners
Updated Jul 8, 2009 by uprime...@gmail.com

JUnit 4.x Quick Tutorial

by Wishnu Prasetya

This tutorial is for novice Java programmers who try to learn JUnit.

How does JUnit work?

JUnit is a library packed in a jar file. Among other things it contains a tool (called test runner) to run your test files. It is not an automated testing tool: you still have to write your test files by hand. JUnit does give you some support so that you can write those test files more conveniently.

Suppose you have a class C that you want to test. We will write the tests in a new class; let’s call it Ctest. This Ctest is our test file. Actually, test class is a better name. We will typically group the tests in Ctest in a bunch of methods called test methods. You will see an example soon.

To actually test C we need to execute its test class Ctest. This is done by calling JUnit’s test runner tool; we pass the name Ctest to it. That’s all. JUnit will then execute Ctest for you.

JUnit will report how many of the test methods in Ctest succeed, and how many fail. The detail of each failure will be reported; this will be in the form of a print of Java’s stack trace leading to the location of the failure.

Locate it first

If you don’t have JUnit yet, you need to download it (from JUnit site). To use it you just need the jar file. A full download also contains its documentation.

Now locate where this jar file is; it is usually called:

junit-<version-number>.jar

To use JUnit you will need to add the full path to this jar to your class path. We will see examples latter.

A simple example

Let us consider the simple class below. This is the class that we want to test; but first let me explain a bit about what it is.

The class is called Subscription; an instance of it represents a subscription to something (e.g. newspaper, but it doesn’t really matter here). Each subscription has its total price, stored in the variable price. This price is in Euro-cent. It also has the length of the subscription, given in months.

public class Subscription {

   private int price ; // subscription total price in euro-cent
   private int length ; // length of subscription in months

   // constructor :
   public Subscription(int p, int n) {
     price = p ;
     length = n ;
   }

  /**
   * Calculate the monthly subscription price in euro,
   * rounded up to the nearest cent.
   */
   public double pricePerMonth() {
     double r = (double) price / (double) length ;
      return r ;
   }

  /**
   * Call this to cancel/nulify this subscription.
   */
   public void cancel() { length = 0 ; }

}

For example, new Subscription(1000,2) will create a new subscription of 1000 Euro-cent for the total period of 2 months.

By the way, the class has a number of bugs; e.g. pricePerMonth is supposed to return the price per month in euro. However it calculates the price in cent.

Let’s write a test

Let us write two simple tests to check if pricePerMonth correctly calculates the price per month:

  1. If we have a subscription of 200 cent for a period of 2 month, its monthly price should be 1 euro, right?
  2. The monthly price is supposed to be rounded up to the nearest cent. So, if we have a subscription of 200 cent for a period of 3 month, its monthly price should be 0:67 euro.

Of course we can write tests without JUnit; but this tutorial is about JUnit. So here is how we write them in JUnit.

Each of the tests above will be implemented as a test method; you then group your test methods in a test class. Usually you would group all the test methods that test a certain target class C is a test class called CTest, but you can also have multiple test classes if you want, and call them whatever you want.

Here is my test class implementing the above two tests:

import org.junit.* ;
import static org.junit.Assert.* ;

public class SubscriptionTest {

   @Test
   public void test_returnEuro() {
      System.out.println("Test if pricePerMonth returns Euro...") ;
      Subscription S = new Subscription(200,2) ;
      assertTrue(S.pricePerMonth() == 1.0) ;
   }

   @Test
   public void test_roundUp() {
      System.out.println("Test if pricePerMonth rounds up correctly...") ;
      Subscription S = new Subscription(200,3) ;
      assertTrue(S.pricePerMonth() == 0.67) ;
   }
}

The marker @Test is called an annotation in Java. When we later execute JUnit’s test runner it needs to know which methods in your test class are test methods (e.g. you may have several helper methods in your test class). The @Test is used to mark that a method is a test method.

In the first test (test_returnEuro) we first create a Subscription; we call it S. Then we want to check that S.pricePerMonth() will return the expected value of 1.0. The checking is done by the code:

assertTrue(S.pricePerMonth() == 1.0)

By the way, the annotation @Test and the method assertTrue are things exported by the JUnit library; so you need the imports as in the above code to use them.

Compiling and executing your test

Next we want to execute the above test class (SubscriptionTest). I’ll show how to do this from a console. If you use an IDE the steps are a bit different.

Open a console. You first need to compile the test class, and then you can execute it. The commands are (in Windows):

prompt> javac -cp .;<full path to JUnit.jar> SubscriptionTest.java

prompt> java  -cp .;<full path to JUnit.jar> org.junit.runner.JUnitCore SubscriptionTest

(In e.g. Mac you probably have to use ':' instead of ';')

The first command will compile the test class. The second will execute JUnit’s test runner; we pass to it the name of your test class.

This is what you will get from JUnit; it reports that both tests fail. Notice also that it reports which lines exactly in your test class fail.

Time: 0,015

There were 2 failures:

   1) test_returnsEuro(SubscriptionTest)
      java.lang.AssertionError:
      ...
      at SubscriptionTest.test_returns_Euro(SubscriptionTest.java:13)
      ...

   2) test_roundUp(SubscriptionTest)
      ...
      at SubscriptionTest.test_roundUp(SubscriptionTest.java:19)
      ...

Tests run: 2, Failures: 2

The next thing you want to do is to fix your Subscription class and then test it again. We keep fixing it until we get no more failures. You may in the mean time also want to add more tests to your test class.

What now!?

That’s it! Now go and write your own tests.

Further reading

  • Eventually you will have to write tests for a serious class. JUnit offers some additional facilities to let you better and more efficiently organize your tests. Unfortunately I don't know a good tutorial for this; so for now I'll just point you to JUnit FAQ.

END.

Comment by benrady, Nov 11, 2008

Why do you prefix your test methods with "test"? What if you used something more expressive?

Comment by project member uprime...@gmail.com, Nov 12, 2008

To benrady:

No reason really, other than it's an old habbit :) I suppose you can name it anything you want as long as it is still within Java's naming convention. So, it can be:

@Test   
public void test_if_pricePerMonth_returns_Euro() { ... }

which is much more informative. Is this what you mean by "more expressive"?

Comment by vinod...@gmail.com, Nov 13, 2008

There seems to be a problem with the test_if_pricePerMonth_returns_Euro() method. Shouldn't it be asserting if S.pricePerMonth() is 1.0 since 200 cents for 2 months comes out to 100 cents per month which is 1.0 euro.

Comment by project member uprime...@gmail.com, Nov 14, 2008

You're right! Thanks!

Comment by sami....@gmail.com, Nov 28, 2008

dude, this is great!!!

java, it seems to me suffers from an inherent lack of decent help - windows has its charles petzold, python has its mark pilgrim ...

thanks

Comment by GeneDeL...@gmail.com, Dec 3, 2008

Java names like test_if_pricePerMonth_returns_Euro() gives me Corba nightmares.

Discover Java naming conventions before publishing next time.

Comment by project member uprime...@gmail.com, Dec 4, 2008

Oh ok ... I'll revert it back to simple name, since it is a tutorial for starters.

For advanced testers, I don't completely agree with you. In a real situation I expect you to have lots of test methods. Java name can be arbitrary long, so that you can embed useful information in it, which you can recover via reflection. This can be useful for e.g. making global reports on your set of test methods.

Comment by rnidef...@gmail.com, Dec 10, 2008

He is not referring to the length of your method names. He is talking about Java conventions for the style of names, i.e:

TestIfPricePerMonthReturnsEuro?()

versus

test_if_pricePerMonth_returns_Euro()

Comment by project member uprime...@gmail.com, Dec 12, 2008

Oooh ... Ok. In that case I find the first rather ugly. At least my poor eyes will have to work twice as hard to auto-insert the spaces.

Comment by bill.tha...@tqs.com, Dec 17, 2008

I think your underscores are fine when describing an action. It gives it a little scheme feel which is comfortable.

Comment by arthurbu...@gmail.com, Dec 17, 2008

Beautiful and straight-forward. Thanks! Passes for

public double pricePerMonth()

{
double r = Math.round(((double) price / (double) length) / 100); return r;
}

Sweet!

Comment by achou...@gmail.com, Dec 23, 2008

hi, i tried to run this example but i had a problem. When i tried to compile the test i took response: javac -cp /usr/share/java/junit4-4.3.1.jar SubscriptionTest?.java

SubscriptionTest?.java:12: cannot find symbol symbol : class Subscription location: class SubscriptionTest?

Subscription S = new Subscription(200,2) ; ^
SubscriptionTest?.java:12: cannot find symbol symbol : class Subscription location: class SubscriptionTest?
Subscription S = new Subscription(200,2) ;
^
SubscriptionTest?.java:19: cannot find symbol symbol : class Subscription location: class SubscriptionTest?
Subscription S = new Subscription(200,3) ; ^
SubscriptionTest?.java:19: cannot find symbol symbol : class Subscription location: class SubscriptionTest?
Subscription S = new Subscription(200,3) ;
^
4 errors

I have create the Subscription class(first program) but as you can see the test program didn't recognize it. Can you help me to run this application?

Comment by project member uprime...@gmail.com, Dec 25, 2008

Hello achourso,

As you compile SubscriptionTest?, the compiler needs Subscription.class but fails to find it. The easiest solution for now is to put both your java files, so Subscription.java and SubscriptionTest?.java in the SAME directory. The compiler will then find your class files without further help.

Of course it is also possible to put them in different directories.

Comment by p...@broadway-performance-systems.com, Dec 25, 2008

You'll want to correct this line:

assertTrue(S:pricePerMonth() == 1.0) // replace colon with period

Comment by project member uprime...@gmail.com, Dec 31, 2008

You're right. Thanks.

Comment by eshar...@mac.com, Jan 2, 2009

To get the commands in the "Compiling and executing your test" section to work, I changed the semicolons to colons. I seen another article that used colons.

http://www.ibm.com/developerworks/library/j-classpath-unix/

Are the semicolons typos?

I'm using Mac OS X.

Comment by lovema...@vip.qq.com, Jan 4, 2009

junit how to it?

Comment by christia...@gmail.com, Jan 4, 2009

@eshar: it depends on your operating system if you should use ':' or ';' to separate entries in your classpath. The commands in this tutorial are for Windows users. For Linux (and Mac) users the commands should be:

prompt> javac -cp .:<full path to JUnit.jar> SubscriptionTest.java

prompt> java  -cp .:<full path to JUnit.jar> org.junit.runner.JUnitCore SubscriptionTest

That should also fix achourso's problem.

Comment by udo.k...@gmail.com, Feb 7, 2009

good and straight to the point

Comment by larachpatino@gmail.com, May 19, 2009

Thank you for writing this quick tutorial, it was really helpful.

Comment by leekiang...@gmail.com, Jul 2, 2009

Really a big Thanks for this tutorial !!

One thing , java convention again ... When I tweak around with this code to test out JUnit for period of times,

Subscription S = new Subscription(200,2) ; assertTrue(S.pricePerMonth() == 1.0) ;

the "S" naming with uppercase letter keep disturbing me to think that the pricePerMonth() is a static method

anyway , not big issue here

again , THANKS !!!

Comment by neenee...@gmail.com, Jul 10, 2009

I think I just need another pair of eyes on this but I am getting an error when I tried to compile SubscriptionTest?. I have everything in the same directory and I downloaded & opened the junit-4.6.jar file, but I keep getting the following errors: "<identifier> expected on the import static org.juinit.Assert.; line 2....

Comment by project member uprime...@gmail.com, Jul 11, 2009

My guess is that you are using java version 1.4; import static is a feature of >= 1.5

Comment by neenee...@gmail.com, Jul 11, 2009

I think it's something else because I typed "C:\>java -version", here at home not at work this time, and got the following results: java version "1.6.0_10" Java(TM) SE Runtime Environment (build 1.6.0_10-b33) Java HotSot?(TM) Client VM (build 11.0-b15, mixed mode, sharing)

by the way here is the entire error printout:

C:\Java Workbench\Projects\EmergencyProc?\SubscriptionTest?.java:1: package org.junit does not exist import org.junit. ; ^ C:\Java Workbench\Projects\EmergencyProc?\SubscriptionTest?.java:2: package org.junit does not exist import static org.junit.Assert. ;

^
C:\Java Workbench\Projects\EmergencyProc?\SubscriptionTest?.java:6: cannot access Test bad class file: .\Test.class class file contains wrong class: org.junit.Test Please remove or make sure it appears in the correct subdirectory of the classpath.
@Test
^

Tool completed with exit code 1

thanks

Comment by project member uprime...@gmail.com, Jul 14, 2009

It looks like different errors are now given. It seems like java has a problem in finding your junit ("...package org.junit does not exist..."). I will have to ask this stupid question :), but do you have junit 4.x included in your classpath?

Comment by neenee...@gmail.com, Jul 16, 2009

I typed "path" at my c prompt and got the following:

PATH=C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\system32\WBEM;C:\Program Files\Java\jdk1.5.0_05\jre\bin;C:\Program Files\Java\jdk1.5.0_05\bin;C:\Program Files\\Maestro Learning\Common;C:\Program Files\QuickTime?\QTSystem\;C:\adabas\bin;C:\adabas\pgm;C:\Program Files\Java\jdk1.6.0_10\bin

It's been awhile since I messed with it but I will obviously have to change it.

thanks

Comment by sebafont...@gmail.com, Jul 23, 2009

Well, there's a bad lesson in here: money shouldn't be stored in a double! That's why someone needed to do some rounds to make it pass. Doubles are for aprox values, and you don't want the aprox amount of euros, you want the exact amount of cents, so store your money in int or long, as cents.

Regards !

Comment by project member uprime...@gmail.com, Aug 1, 2009

Well, in the example it is NOT stored in double, but in int. The class just have a function that express it in double.

Comment by SSPri...@gmail.com, Aug 27, 2009

Hey, how to generate reports on the list of test cases that failed /passed? Also, is there a way to run only the failed test cases again and merge the results?

Comment by eatest.i...@gmail.com, Sep 10, 2009

First uprime812, i want to thank you so much! Most Junit tutorials are aloof and a little intimidating. The few minutes i spent with yours has really helped me and boosted my confidence. Thank you.

Second, in the compiling and execution test section, i entered in my cmd prompt and received the following: C:\junit\junit4.7>javac -cp .;C:\junit\junit4.7 junit_tut1_tc.java javac: file not found: junit_tut1_tc.java Usage: javac <options> <source files> use -help for a list of possible options

Is their a specific place my file/class junit_tut1_tc.java has to be?

Thanks, MrBCut

Comment by joseph.a...@gmail.com, Oct 8, 2009

C:\junit\junit4.7>javac -cp .;C:\junit\junit4.7 junit_tut1_tc.java

don't forget the jar C:\junit\junit4.7>javac -cp .;C:\junit\junit4.7\junit<version>.jar junit_tut1_tc.java

Comment by billnels...@gmail.com, Oct 15, 2009

Thanks for the quick and straight forward tutorial. Very nice jumpstart for an agile newbie.

Comment by bahe...@gmail.com, Nov 16, 2009

I get the following error: java.lang.Exception: Test class should have public zero-argument constructor

any suggestions?

Comment by frankgroeneveld, Nov 23, 2009

Make sure the constructor of you test class doesn't need any arguments.

Comment by sunny.ii...@gmail.com, Feb 17, 2010

hi i want to run same testcase for multiple times with different data

for example if i run the same testcase for 3 times with different data it should show

Tests run: 3,failures : 0

when i tried am getting Tests run : 1 only any suggestions?

Comment by sachin.s...@gmail.com, May 5, 2010

Good tut...

sachin sthalekar

Comment by mianas...@gmail.com, May 11, 2010

Excellent job, I am onto your other tutorials

Comment by jothi.ni...@gmail.com, Jul 18, 2010

Good Tutorial

Comment by sagathik...@gmail.com, Jul 26, 2010

good tutorial

Comment by jamm...@gmail.com, Sep 12, 2010

really good tutorial, but it's not very detail referenced to ignore, time test. More details in http://www.javarmi.com/2010/09/junit-tutorials-and-example

Comment by marimuth...@gmail.com, Sep 24, 2010

Hi Friends, can Any 1 help Me , Why I am getting this error teven though I set path.

C:\Program Files\Java\jdk1.6.0_05\bin>set path="C:\Documents and Settings\220805 \Desktop\junit-4.8.1"

C:\Program Files\Java\jdk1.6.0_05\bin>javac CalculatorTest?.java CalculatorTest?.java:1: package org.junit does not exist import org.junit. ; ^ CalculatorTest?.java:2: package org.junit does not exist import static org.junit.Assert. ;

^
CalculatorTest?.java:8: cannot find symbol symbol: class TestCase? public class CalculatorTest? extends TestCase? {
^
CalculatorTest?.java:16: cannot find symbol symbol : method assertEquals(int,int) location: class CalculatorTest?
assertEquals(2,cal.sum(1,1)); ^
4 errors

Comment by jsund...@gmail.com, Nov 26, 2010

good tutorial

Comment by laran...@gmail.com, Nov 30, 2010

When I tried this example I get this when trying to compile:

==============================================0 ~/java/test$ javac SubscriptionTest?.java SubscriptionTest?.java:7: incompatible types found : Test required: java.lang.annotation.Annotation

@Test
^
SubscriptionTest?.java:14: incompatible types found : Test required: java.lang.annotation.Annotation
@Test
^
2 errors

I cannot see what I have done wrong. I'm using junit-4.8.2.jar and openjdk-6-jdk.

Thank's for the otherwise very good starter.

Comment by mangeshm...@gmail.com, Dec 6, 2010

Thank you for starter on JUnit 4.x.

Classes Subscription and SubscriptionTest? got compiled and executed successfully.

Similar test code I have tried is not getting compiled. It shows 'cannot find symbol' error as shown below:


AnimalTest??.java:11: cannot find symbol symbol : method assetEquals(java.lang.String,java.lang.String,java.lang.String)

location: class AnimalTest??

assetEquals("String returned is not matching with expected", "sound of animal", animalToBeTested.talk()); ^

AnimalTest??.java:18: cannot find symbol symbol : method assetEquals(java.lang.String,java.lang.String,java.lang.String)

location: class AnimalTest??

assetEquals("String returned is not matching with expected", "Animal eats food for animal", animalToBeTested.eat("food for animal")); ^ 2 errors


Code of AnimalTest? is as follows:


import org.junit.; import static org.junit.Assert.;

public class AnimalTest? {

@Test public void testTalk() {
Animal animalToBeTested = new Animal(); assetEquals("String returned is not matching with expected", "sound of animal", animalToBeTested.talk());
}
@Test public void testEat() {
Animal animalToBeTested = new Animal(); assetEquals("String returned is not matching with expected", "Animal eats food for animal", animalToBeTested.eat("food for animal"));
}

}


Corresponding class Animal got compiled and executed successfully (it has main method).

Files Animal.java, Animal.class and AnimalTest?.java are in same directory.

How to resolve the error ?

Comment by mangeshm...@gmail.com, Dec 6, 2010

I have java version 1.6.0_21 and JUnit version 4.7

Comment by christia...@gmail.com, Dec 6, 2010

@mangeshminal The error tells you that there's no assetEquals method, which is true since it's assertEquals.

Comment by mangeshm...@gmail.com, Dec 6, 2010

Thank you @christiaanhees for pointing out the error. It compiled and executed successfully.

Comment by jwasan...@gmail.com, Dec 9, 2010

>>Open a console. You first need to compile the test class, and then you can execute it. The commands are (in Windows):

What happen the normal class (Subscription) it want compile or what? When I compile test class SubscriptionTest?.java:16: cannot find symbol symbol : class Subscription location: class SubscriptionTest?

Subscription S = new Subscription(200,3) ;
^

Comment by v111ram...@gmail.com, Jan 17, 2011

i have successfully compiled the Subscription.java & SubscriptionTest?.java files and get the Class files when i Run the SubscriptionTest? file then i get the message as Exception in Thread "main" java.lang.NoClassDefFoundError?: but i have both the Class file at the current Directoty.

plz help in this

Comment by jason.th...@gmail.com, Mar 15, 2011

assertTrue(S.pricePerMonth() == 0.67);

Please don't perpetuate this practice. Always use assertEquals when you are checking for equality. The framework will give you much better information if you work with it. For example if you used:

assertEquals(0.67, S.pricePerMonth());

If this fails junit will tell you your expected and actual results without having to look at the code. Yours will simply tell me it failed.

Comment by project member uprime...@gmail.com, Mar 30, 2011

Jason, I think that difference is really superficial. Most likely you will have to inspect the test-case anyway to understand why it fails. Writing it as assertTrue(e1 == e2) makes the expression reads better. Since most of my effort goes to thinking about the logic of the test and later, if it fails, in figuring out why, I really favor code readability.

Comment by KnightL...@gmail.com, Apr 11, 2011

good article!

Comment by gogatesu...@gmail.com, May 5, 2011

how can i write unit test cases for web application ? mainly i would like to know how we can pass HttpServletRequest? object or database matadata to test case?

Comment by kat...@gmail.com, Apr 7, 2012

one of the best tutorials i've read in months! thank you very much

Comment by umadevi....@gmail.com, Apr 19, 2012

How to make an ordered execution of testcases? Example testcase1.create,testcase2.update , ,testcase3.delete.I want to run create followed by update and finally delete.But i got create,delete,update as an execution order.

Comment by shrikant...@gmail.com, Dec 11, 2012

hey buddy, you should also provide the TestSuite? implementation ,,,

Comment by sumeshba...@gmail.com, Dec 24, 2012

Hello, For all junit testers, some good info is available on my blog regarding junit assertion failures. http://apitestingwithjunit.blogspot.in/

Comment by shrikant...@gmail.com, Dec 25, 2012

HI GUYS ,I am Providing you the complete Solution for this : KEEP CODING

Note : Just Create hierarchy :

E:. │ .classpath │ .project │ projectSet.psf │ ├───.settings │ org.eclipse.jdt.core.prefs │ ├───bin │ └───com │ └───cxt │ └───java │ ├───source │ │ Bank.class │ │ │ └───test │ BankTest?.class │ BankTestSuite?.clas │ MyTestRunner?.class │ └───src

└───com
└───cxt
└───java
├───source │ Bank.java │ └───test
BankTest?.java BankTestSuite?.java MyTestRunner?.java


STEP 1 : CREATE THE SOURCE PROGRAM.

package com.cxt.java.source;

public class Bank {

public long accno; public double amount; public double withdraw;

static double security_amount=1000.00;
public double depositeMoney(long accountno, double amount) {
this.accno=accountno; this.amount=amount; this.amount+=amount; System.out.println("Amount "+amount+" has been successfully deposited into A/C Number : "+accountno); System.out.println("Available Balence : "+this.amount+" & Security Deposite : "+security_amount); return amount;
}
public double withdrawMoney(long accountno, double amount) {
this.accno=accountno; this.amount=amount; this.amount-=amount;
if (this.amount<=amount && this.security_amount >= amount) {
System.out.println("Withdrawing form the Security Deposite Ammount"); this.security_amount-=amount; System.out.println("Amount $ "+amount+" has been successfully withdrawed from A/C Number : "+accountno);
} else {
System.out.println("The enterd amount is bigger than the available as & security deposited amount.\n Hence Can't be withdraw."); System.out.println("Amount $ 00.00 has been successfully withdrawed from A/C Number : "+accountno);
}
System.out.println("Available Balence : "+this.amount+" & Security Deposite : "+security_amount); return this.amount;
}
public double checkBalence() {
System.out.println("Current A/C Balence is :"+this.amount+" in "+this.accno+" A/C number");
return this.amount;
}
}

  • STEP 2 : CREATE TEST CLASS.

package com.cxt.java.test;

import static org.junit.Assert.assertEquals;

import org.junit.After; import org.junit.AfterClass?; import org.junit.Before; import org.junit.BeforeClass?; import org.junit.Ignore; import org.junit.Test;

import com.cxt.java.source.Bank;

public class BankTest? {

static Bank bank;

@BeforeClass? public static void setUpBeforeClass() throws Exception {
if (bank==null) {
bank=new Bank();
} else {
System.out.println("Bank object should be singleton in nature..");
}
}

@AfterClass? public static void tearDownAfterClass() throws Exception {
bank=null; System.gc();
}
@Before public void setUp() throws Exception {
System.out.println("setUp()");
}
@After public void tearDown() throws Throwable {
System.out.println("tearDown()\n"); //System.gc();
}

@Test public final void testDepositeMoney() {
long accno=1001; double amount=1000.00; double expected=amount; double actual=bank.depositeMoney(accno, amount); assertEquals("Exception raised in testDepositeMoney()",expected,actual,0.0);
}

@Test public final void testWithdrawMoney() {
long accno=1000; double amount=1000.00; double expected=0.0; double actual=bank.withdrawMoney(accno, amount); assertEquals("Exception Raised into testWithdrawMoey()",expected, actual,0.0);
}
@Test public final void testWithdrawAccessMoney() {
long accno=1001; double amount=100000.00; double expected=0.0; double actual=bank.withdrawMoney(accno, amount); assertEquals("Exception Raised into testWithdrawMoey()",expected, actual,0.0);
}

}

  • STEP 3 : CREATE TEST SUITE CLASS

package com.cxt.java.test;

import org.junit.runner.RunWith?; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses?;

import junit.framework.TestResult?; import junit.framework.TestSuite?;

@RunWith?(Suite.class) @SuiteClasses?({

BankTest?.class
}) public class BankTestSuite? {

}

STEP 4 : CREATE TEST RUNNER

package com.cxt.java.test;

import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure;

public class MyTestRunner? {

public static void main(String args) {
Result result = JUnitCore.runClasses(BankTestSuite?.class); for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
} System.out.println("Was Successful.?"+result.wasSuccessful()); System.out.println("Total Run Count : "+result.getRunCount());
System.out.println();System.out.println();System.out.println(); System.out.pritnln("... Jay Maharastra ...");
}

}

Comment by sonu4...@gmail.com, Mar 11, 2013

For this --- public class Subscription { private int price ; // subscription total price in euro-cent private int length ; // length of subscription in months // constructor : public Subscription(int p, int n) { price = p ; length = n ; } / Calculate the monthly subscription price in euro, rounded up to the nearest cent. / public double pricePerMonth() { double r = (double) price / (double) length ; return r ; } / Call this to cancel/nulify this subscription. / public void cancel() { length = 0 ; } }

For getting 0.67...we need to modify the pricePerMonth() method You have to use double r = Math.rint((double) price / (double) length)/100 ;...

Thanks Sonu

Comment by nevil...@gmail.com, Mar 17, 2014

Junit Tutorials Series On Techno Nutty Junit Tutorials Series


Sign in to add a comment
Powered by Google Project Hosting