Prev Next

Testing / Junit Interview questions

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is Java "assert" statement?

The assert keyword is used in assert statement which is introduced in Java 1.4.

It helps the code developers to test assumptions and to assert if the outcome is as expected that enables detecting bug and fixing it.

public class AssertExample {

	public static void main(String[] args) {
		assert 0 > 1;
		System.out
				.println("This line is printed since -ea or -enableassertions is not enabled as VM argument.");

	}
}

Format:

assert condition;
assert boolean_expression : string_expression;

assert <condition>: "Error message to display when condition failed";

When this statement is executed:

  • If boolean_expression evaluates to true, the statement will pass normally.
  • If boolean_expression evaluates to false, the statement will fail with an "AssertionError"

2. What is the "if statement" equivalent of Assert.

if (!<condition>)
throw new AssertionError();

3. What is unit testing?

Unit Testing refers to the process of verifying/testing the individual components, functional units of any application (methods/procedures/classes) that contains business logic, reusable components and ensure the working of the functional units.

Unit testing can be done automatically or manual.

4. Differentiate Manual vs Automated Unit testing.

Manual testing involves the preparation of test cases and those are tested by developers.

Automated Unit testing: Test cases are executed by automation tools such as JUnit.

Automated Unit testing is preferred over manual, as Automated test helps covering all the scenario/test any time required. Automated unit testing is fast compared to Manual, less expensive and accurate.

5. Unit Test Case.

refers to the combination of possible inputs to the unit and desired output of based on the input, that helps to certify the functionality of the individual unit.

Unit test cases are prepared and used by Developers.

6. Difference between JUnit3 and JUnit4.

JUnit4 relies on annotations whereas JUnit3 use method names to identify tests.

7. What is JUNIT?

Junit is an open source testing framework developed for unit testing Java based applications.

An API that enables developer to writes and executed repeatable automated test cases.

8. Advantages of using Junit.

Enables writing test cases while developing the software that helps test early and detect issues.

Ensure the functionality is performing as expected every time the code is modified by the use of repeatable automated test cases.

supported by all IDE including Eclipse, Netbeans, RAD etc.

integrates with Ant and Maven(CI) that enables execuition of test suites or test cases as part of the build process, capturing test result and reporting.

accurate and efficient testing process.

9. How will you create a JUnit Test Class?

in JUnit3, Java classes should extend junit.framework.TestCase .

JUnit 4, any public class with a no arg public constructor can behave as a JUnit test class.

10. How do you create a JUnit test Method?

in JUnit3, test methods should follow the naming convention as the pattern test for e.g. testCompareMethod.

In Junit 4, test methods would be just annotated with @Test.

11. Can a JUnit test method have a return type other than void?

All the JUnit test methods should have a void return type. otherwise it will be ignored.

If the return type of a test method is changed, it would not be considered as a test method and would be ignored during execution of tests.

12. Other constraints for creating a Junit test method.

All test methods must be declared as public.

13. What is the latest version of Junit.

Junit5 published on March 21, 2016.

14. Differentiate JUnit 4 and TestNG.

in Java, JUnit 4 and TestNG are popular unit test framework and look very similar in functionality.

TestNG supports annotation @BeforeSuite that enables running a method before even the suite runs whereas Junit 4 does not support.

TestNG supports @AfterSuite that enables running a method after the suite completes running whereas Junit 4 does not support.

Similarly TestNG adds support for test specific and groups specific before (setup )and After (teardown) annotation whereas Junit does not support.

15. What is @Test annotation?

@Test annotation is used to denote that the method is a test method.

16. Explain @Ignore.

@Ignore annotation ignore the annotated test method to be executed.

It is used when the test method is incomplete or needed to be not executed.

17. What are the annotations used in Junit4?

@Test.

The Test annotation indicates that the public void method to which it is attached can be run as a test case.

@Before.

The Before annotation indicates that this method must be executed before each test in the class, so as to execute some preconditions necessary for the test.

@BeforeClass.

The BeforeClass annotation indicates that the static method to which is attached must be executed once and before all tests in the class.

@After.

The After annotation indicates that this method gets executed after execution of each test.

@AfterClass.

The AfterClass annotation can be used when a method needs to be executed after executing all the tests in a JUnit Test Case class so as to clean-up the set-up.

@Ignores.

The Ignore annotation can be used when you want temporarily disable the execution of a specific test.

18. Exception testing (Difference between Junit 3 and Junit 4)

Junit 3

 public void testGetElement() {
  try {
    (new ArrayList()).get(0);
    fail("The test should have failed");
  } catch (NullPointerException e) {
    assertTrue("Sanity check", true);
  }
}

Junit 4

Junit 4.x

@Test(expected = IndexOutOfBoundsException.class)
public void testGetElement() {
  (new ArrayList()).get(0);
}

19. Difference between @Before and @BeforeClass.

@Before
public void runBeforeEachTest()

This method gets executed before each test execution in the current Test class. This method usually helps initialize the resources required for the action test to execute. It is executed everytime before running a test method.

@BeforeClass
public static void runBeforeAllTheTest()

This method executes before running any of the test in the current Test Class. It is executed only once.

Used for setuping database connectivity before running any of the test.

This method should be declared static whereas @Before don't have to be.

20. Differentiate @After and @AfterClass.

@After
public void runAfterEachTest()

This method gets executed after every test and it is used to clean up the test and temporary data that are used by test.

@AfterClass
public static void runAfterAllTheTest()

This method gets executed at the end, when all the tests have completed execution and gets executed only once.

Usually used for closing the database connection.

Method should be declared static.

«
»
Junit Interview questions II

Comments & Discussions