Sunday, October 16, 2016

JUnit and Wrapper class in Java

Features of JUnit

  • JUnit is an open source framework, which is used for writing and running tests.
  • Provides annotations to identify test methods.
  • Provides assertions for testing expected results.
  • Provides test runners for running tests.
  • JUnit tests allow you to write codes faster, which increases quality.
  • JUnit is elegantly simple. It is less complex and takes less time.
  • JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.
  • JUnit tests can be organized into test suites containing test cases and even other test suites.
  • JUnit shows test progress in a bar that is green if the test is running smoothly, and it turns red when a test fails.

Features of JUnit Test Framework

  • Fixtures-Fixtures is a fixed state of a set of objects used as a baseline for running tests
  • Test suites-A test suite bundles a few unit test cases and runs them together.
  • Test runners-Test runner is used for executing the test cases.
  • JUnit classes
    • Assert − Contains a set of assert methods.
    • TestCase − Contains a test case that defines the fixture to run multiple tests.
    • TestResult − Contains methods to collect the results of executing a test case.

 

Assert Class

Following is the declaration for org.junit.Assert class −

public class Assert extends java.lang.Object

This class provides a set of assertion methods useful for writing tests. Only failed assertions are recorded. Some of the important methods of Assert class are as follows −

Methods & Description   
  • void assertEquals(boolean expected, boolean actual)-Checks that two primitives/objects are equal.   
  • void assertFalse(boolean condition)-Checks that a condition is false   
  • void assertNotNull(Object object)-Checks that an object isn't null.  
  • void assertNull(Object object)-Checks that an object is null   
  • void assertTrue(boolean condition)-Checks that a condition is true.   
  • void fail()-Fails a test with no message.

 

Annotation

Annotations are like meta-tags that you can add to your code, and apply them to methods or in class. These annotations in JUnit provide the following information about test methods (mainly it can be used to set execution procedures)−
  1. @Test-The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.
  2. @Before-Several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before each Test method.
  3. @After-If you allocate external resources in a Before method, you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method.
  4. @BeforeClass-Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class.
  5. @AfterClass-This will perform the method after all tests have finished. This can be used to perform clean-up activities.
  6. @Ignore-The Ignore annotation is used to ignore the test and that test will not be executed.

Ignore Test

A test method annotated with @Ignore will not be executed. 

If a test class is annotated with @Ignore, then none of its test methods will be executed. 

 @Test(timeout = 1000)
   public void testPrintMessage() {  
      System.out.println("Inside testPrintMessage()");   
      messageUtil.printMessage();   
   }

 

Exceptions Test 

JUnit provides an option of tracing the exception handling of code. You can test whether the code throws a desired exception or not.

@Test(expected = ArithmeticException.class)
   public void testPrintMessage() {   
      System.out.println("Inside testPrintMessage()");    
      messageUtil.printMessage();    
   }


Time Test 

JUnit provides a handy option of Timeout. If a test case takes more time than the specified number of milliseconds, then JUnit will automatically mark it as failed. 


Parameterized Test

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test.

Wrapper class in Java

  • Wrapper class in java provides the mechanism to convert primitive into object and object into primitive.
  • Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known and autoboxing and vice-versa unboxing.
  • One of the eight classes of java.lang package are known as wrapper class in java. The list of eight wrapper classes are given below:
Primitive Type     Wrapper class
boolean              Boolean
char                   Character
byte                   Byte
short                  Short
int                     Integer
long                  Long
float                  Float
double              Double

  • public class WrapperExample1{  
  • public static void main(String args[]){  
  • //Converting int into Integer  
  • int a=20;  
  • Integer i=Integer.valueOf(a);//converting int into Integer  
  • Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally  
  •   
  • System.out.println(a+" "+i+" "+j);  
  • }}  

  • public class WrapperExample2{    
  • public static void main(String args[]){    
  • //Converting Integer to int    
  • Integer a=new Integer(3);    
  • int i=a.intValue();//converting Integer to int  
  • int j=a;//unboxing, now compiler will write a.intValue() internally    
  •     
  • System.out.println(a+" "+i+" "+j);    
  • }}    

No comments:

Post a Comment