JUnit – Heads up

JUnit

  • JUnit is a unit testing framework for the Java programming language
  • JUnit has been important in the development of test-driven development (TDD), and is one of a family of unit testing frameworks(xUnits)
  • It is not an automated testing tool, we still have to write our test files by hand
  • JUnit does give us some support so that we can write those test files more conveniently

Problems in testing the applications

  • In a web application, to test the flow we need to deploy it in the server so if there is a change then again we need to restart the server
  • There is no explicit concept of a test passing or failing
  • There is no mechanism to collect results in a structured fashion
  • More time consuming in Trial and error approach development 

Advantages of JUnit

  • Using JUnit we can save testing 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
  • No need to build and deploy each time for a minor change
  • Using JUnit, If we want to test the application (for web applications) then server is not required so the testing becomes fast 

Members of JUnit

TestSuite

Collection of TestCases, Running a test suite will execute all test classes in that suite in the specified order

TestCase

  • A formal written unit test case is characterized by a known input and by an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a postcondition
  • There must be at least two unit test cases for each requirement

Assertion

  • These assertion methods typically start with assert and allow you to specify the error message, the expected and the actual result
  • An assertion method compares the actual value returned by a test to the expected value, and throws an AssertionException if the comparison test fails

junit-class_diagram

Test Suite Implementations

A test suite class can be implemented in two ways:

  • Using TestRunners (org.junit.runner.JUnitCore)
  • Using Annotations

TestRunner

public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestClassOne.class,
TestClassTwo.class);
}

Annotations

@RunWith(Suite.class)
@Suite.SuiteClasses({ TestClassOne.class,
TestClassTwo.class})
public class JunitTestSuite {
}

Test Case Implementations

 A test case class can be implemented in two ways:

  • Extending TestCase(junit.framework.TestCase)
  • Using Annotations

Extending TestCase

public class DemoTest extends TestCase{
public void test() {
fail(“Failed”);

}

Using Annotations 

@Test
public void testPrintMessage() {
System.out.println(“Inside testPrintMessage()”);
assertEquals(message, messageUtil.printMessage());
}

References:

Thanks 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s