ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Business and Employment»
  • Employment & Jobs»
  • Interviewing for a Job

Interview questions and answers of Unit Testing with JUnit, important notes of JUnit and simple examples of JUnit

Updated on December 29, 2013

This page is for

Java developers

  • learn and answer unit testing basics
  • integrate JUnit and practice
  • Beginner level knowledge of JAVA

What is JUnit?

  • JUnit is a unit testing framework for Java.

  • A programmer-oriented testing framework for Java.

    [JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.]

What is software testing/ testing?


Software testing / testing is the process of checking all the functionality of the application whether it is working as per requirements.

What can you do using JUnit?

Write tests, run them, see results!

Is JUnit is necessary to test in Java?

No, it's not mandatory. We can write our own Java code to test.

Is JUnit testing a development activity?

Creating unit testing in JUnit is a development activity or testing activity

Does JUnit do testing by itself?

No. It just gives us the easy way to integrate tests and run those tests

How JUnit identifies the test cases?

We use annotation to write any test case. So, whenever JUnit finds any annotation, it understands that it is a test case.

What are the features of JUnit?

  • Asserts
  • Test setup and tear-down
  • Exception testing
  • Test suites
  • Parametrized testing
  • Assumptions
  • Rules
  • Theories
  • Integration with popular build systems

What is the smallest "unit" in Java?

↓In Java, the smallest "unit" is a class.

What are the ways of getting JUnit?

  • Build in Eclipse
  • GitHub download

Name some JUnit Annotations

	@Test
	@Before
	@After
	@BeforeClass
	@AfterClass
	@Ignore
	@Test(expected = Exception.class)
	@Test(timeout=100)

What is JUnit Test methods?

In order to create unit test in JUnit, we need to create a test method. So, it's the method to test. The method signature is public and a void return type. We also need to add annotation @Test to make sure that the JUnit runner can easily recognize the test.

        @Test
	public void testMethodName() {
		// fail("Not yet implemented");
	}

What is Manual testing?

If the test cases of software testing are executed manually, without using any tool support, then it is known as manual testing.

Manual Testing has the following features:

  • Time consuming and tedious
  • Huge investment in human resources
  • Less reliable
  • Non-programmable

What is Automated testing?

Taking tool support and executing the test cases by using automation tool is known as automation testing.

Automated testing is:

  • Fast
  • Less investment in human resources
  • More reliable
  • Programmable

JUnit Timeline

Year
Details
1994
SUnit Created
1997
JUnit started
2000
JUnit.org
202
Eclipse IDE Support

Steps to create a HelloJUnit test

Suppose, we have a Java project Calculator which has one method to add two integer values and give the output by adding those two numbers. Now, we want to create a JUnit test for this project. The steps are:

  1. Create a new project like CalculatorTest
  2. Add a New JUnit Test Case and save it with the name HelloJUnit
  3. Run the code as JUnit Test

Full code of HelloJUnit

package com.hubpages.plusminus;

import static org.junit.Assert.*;

import org.junit.Test;

public class HelloJUnit {

	@Test
	public void test() {
		// fail("Not yet implemented");
	}

}

JUnit is a unit testing framework for Java. [Write tests, run them, see results!]. JUnit framework doesn't do testing. It gives you the easy way to test / integrate test

Example: Basic JUnit test : Creating a simple test

We have a method sumResult which can adds two string values and return the integer output. We will test whether the method is working properly or not.

The sumResult method is defined into a project named as Calculator. The full snapshot of this project and method is in the following figure

Full code of sumResult

public class SumMethod {

	public int sumResult(String x1, String x2) {

		int a1 = Integer.parseInt(x1);
		int a2 = Integer.parseInt(x2);
		int result = 0;

		result = a1 + a2;

		return result;
	}
}

Now our plan is to test the functionality of this method. To do this we have to do the following steps:

Step 1: Creating a new project

Create a new Java project into Eclipse and name it as CalculatorTest or whatever you like.

After completing the creation of Java project, we will see the Java project appears into he Project Explore panel.

Step 2: Adding the project reference

Our goal is to test the method of Calculator project. And all our testing code should reside into CalculatorTest project. That's why, we need to create a project reference. We will create a project reference by configuring the build path properties like the following figures:

Step 3: Creating a JUnit java file

We need to create JUnite Test case. So, we choose a JUnit Test Case type of Java file and create that file. Suppose the name of the java file is SumMethodTest. We will use this method to test the sum functionality of the SumMethod. At the time of creating this JUnit test case, do not forget to choose the JUnit version, mentioning the package name. After doing these, click on Finish button to complete the creation of JUnit Test case file.

The final outlook of the project will be like the following after the above steps.

Step 4: Cleaning all the code

Remove all the codes inside SumMethodTest class. The final codes will look like the following lines of code.

package com.hubpages.plusminus;

public class SumMethodTest {


}

Step 5: Creating a test case method

Now, we will create a method to test. Remember, we must have to include a static import of Assert. The full code of the method implementation will be like the following:

package com.hubpages.plusminus;

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

public class SumMethodTest {

	@Test
	public void SumMethodFunctionalityTesting() {

		SumMethod sumMethod = new SumMethod();
		assertEquals("Sum is 100", 100, sumMethod.sumResult("90", "10"));

	}

}

Step 6: Run the project to test

Right click onto the code and choose Run As and then choose JUnit Test to start running the project. When the project runs completely, you will see the following output:

Step 7: Modify the project code and test whether it fails?

This step is not necessary actually. Yet, we want to see what happens if the method fails, what JUnit shows us. To do this we modify our code like the following:

package com.hubpages.plusminus;

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

public class SumMethodTest {

	@Test
	public void SumMethodFunctionalityTesting() {

		SumMethod sumMethod = new SumMethod();
		assertEquals("Sum is not 100", 50, sumMethod.sumResult("90", "10"));

	}

}

And now we run the project and see the following output:

So, we are seeing that JUnit really shows the correct output if the method really fails.

Example: Creating multiple tests

We will test multiple methods by adding test codes into the same test file. We have a class CalculationMethods which has four methods. The class is like the following:

package com.hubpages.plusminus;

public class CalculationMethods {

	public int addition(String x1, String x2) {

		int a1 = Integer.parseInt(x1);
		int a2 = Integer.parseInt(x2);
		int result = 0;

		result = a1 + a2;

		return result;
	}

	public int subtraction(String x1, String x2) {

		int a1 = Integer.parseInt(x1);
		int a2 = Integer.parseInt(x2);
		int result = 0;

		result = a1 - a2;

		return result;
	}

	public int multiplication(String x1, String x2) {

		int a1 = Integer.parseInt(x1);
		int a2 = Integer.parseInt(x2);
		int result = 0;

		result = a1 * a2;

		return result;
	}

	public float division(String x1, String x2) {

		float a1 = Float.parseFloat(x1);
		float a2 = Float.parseFloat(x2);

		float result = 0;

		result = a1 - a2;

		return result;
	}
}

We will test the methods addition method and multiplication method. To do this, we have to write to test case. Suppose the test cases are additionMethodTestForValues50And50 and multiplicationMethodTestForValues10And20. The first method will test whether the method is successful to add the values 50 and 50 to provide the result 100. The second method will test the result 200 with the multiplication of two values 10 and 20. Full codes of these testing cases are the following:

package com.hubpages.plusminus;

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

public class CalculationMethodsTest {

	@Test
	public void additionMethodTestForValues50And50(){
		CalculationMethods method = new CalculationMethods();
		assertEquals("Sum of 50 and 50 is not 100", 100, method.addition("50", "50"));
	}
	
	@Test
	public void multiplicationMethodTestForValues10And20(){
		CalculationMethods method = new CalculationMethods();
		assertEquals("Multiplication of 10 & 20 is not 200" ,200, method.multiplication("10", "20"));
	}
	
	
}

Project structure

Test case output result:

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Emma 3 years ago

      Great job!