More you share, more you have!!!

More you share, more you have!!!

How to perform unit testing with Junit testing library in java environment

junit-java-unit-testing

Q.How to perform unit testing using Junit testing library in java environment.

CLASS TO BE TESTED :

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,

   */
   public double pricePerMonth() {
     double r =((double) price / (double) length) ;

      return r ;
   }

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


}

TEST CASE :


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,5) ;
System.out.println(S.pricePerMonth());
      assertTrue(S.pricePerMonth() == 0.4) ;
   }

   }


OUTPUT :


INSTALLING JUNIT&RUNNING TEST CASE:
 

F:\dk>javac -cp .;JUnit.jar SubscriptionTest.java

F:\dk>java -cp .;JUnit.jar org.junit.runner.JUnitCore SubscriptionTest
JUnit version 4.8.1
.Test if pricePerMonth returns Euro...
40.0
E
Time: 0.016
There was 1 failure:
1) test_returnEuro(SubscriptionTest)
java.lang.AssertionError:
        at org.junit.Assert.fail(Assert.java:91)
        at org.junit.Assert.assertTrue(Assert.java:43)
        at org.junit.Assert.assertTrue(Assert.java:54)
        at SubscriptionTest.test_returnEuro(SubscriptionTest.java:11)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcce
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(Framewor
Method.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCa
lable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkM
thod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMe
hod.java:20)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRu
ner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRu
ner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:24)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
        at org.junit.runner.JUnitCore.runMain(JUnitCore.java:98)
        at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
        at org.junit.runner.JUnitCore.main(JUnitCore.java:45)

FAILURES!!!
Tests run: 1,  Failures: 1



CorrectedProgram:-

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,

   */
   public double pricePerMonth() {
     double r =((double) price / (double) length)/100 ;

      return r ;
   }

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

}




TEST CASE :

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,5) ;
System.out.println(S.pricePerMonth());
      assertTrue(S.pricePerMonth() == 0.4) ;
   }

   }



OUTPUT :


INSTALLING JUNIT&RUNNING TEST CASE:

F:\dk>javac -cp .;JUnit.jar SubscriptionTest.java

F:\dk>java -cp .;JUnit.jar org.junit.runner.JUnitCore SubscriptionTest
JUnit version 4.8.1
.Test if pricePerMonth returns Euro...
0.4

Time: 0

OK (1 test)


F:\dk>

Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment