Prev Next

Testing / Junit Interview questions III

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

1. Explain parameterized tests in Junit.

In Junit 4, a new feature named "parameterized tests" is introduced that allows developer to run the same test repeated for different input values.

Creating parameterized tests using Junit 4 is simple and the steps are as follows.

  • Annotate the Junit test class with @RunWith (Parameterized.class).
  • Create a public static method and annotate with @Parameters that returns a Collection of Object array to be used as test dataset organized into rows and columns.
  • Create a public constructor that accepts one row equivalent of test data as parameter.
  • Create an instance variable for each column/field of a row test data.
  • Create your test cases using the instance variables (columns) as the input test data.

For each row of test data, the test case will be invoked and executed.

2. When are unit tests garbage collected in Junit?

As per the design, the Test instances tree is built at first pass and the test execution is performed as the second pass.

The test runner holds strong references to all test instances for the duration of the test execution. For instance, in case of a very long test run with many Test instances, none of the tests may be garbage collected until the completion of the entire test run.

Therefore, we need to programmatically free-up resources that are availed or allocated for running a test.. Explicitly setting an object to null in the tearDown() method allows it to be garbage collected before the completion of the entire test run.

3. What are the different methods of exception handling in JUnit?

There are different ways of exception handling in JUnit.

  • Try catch idiom.
  • Using JUnit rule.
  • @Test annotation.
  • Using catch exception library.
  • Using customs annotation.
4. Explain the steps involved in writing a simple JUnit test case.

Create a subclass of TestCase.

To initialize object under test, override the setup() method.

To release object under test, override the teardown() method.

Create one or more public testMethodname () methods that assess the objects under test and assert expected results.

5. What is @ignore test annotation in JUnit framework?

When the junit test code is not complete and it could fail if executed, then it can be annotated using @Ignore annotation to prevent the test method/class from execution.

  • When @Ignore applied to a test method, it prevents the method from execution while other methods in the class gets executed.
    @Ignore @Test public void testMethod() { ...
    

  • When @Ignore applied to a test class, none of the test methods within the test class gets executed.
    @Ignore public class JunitClass {
                            @Test public void test1() { ... }
                            @Test public void test2() { ... }
                    }
    

@Ignore takes an optional default parameter if you want to mention or log why a test is being ignored.

@Ignore("yet to complete the test.") @Test public void testMethod() { ...

6. How do I execute JUnit from command line?

Executng Junit from command line involves 2 steps.

1. set the ClassPath to include JUnit core libraries.

set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%junit.jar

2.Invoke JunitCore.

java org.junit.runner.JUnitCore

7. Junit: What is @Rule annotation?

@Rule annotation in Junit is used to create objects that later can be used with the test methods.

Junit @Rule annotation works on the principle of AOP (aspect oriented programming). It intercepts the test method thus providing an opportunity perform some action before or after the execution of a particular test method.

public class JunitRuleExampleTest {

  @Rule
  public TemporaryFolder tFolder = new TemporaryFolder();

  @Test
  public void testRule() throws IOException {
    File txtFile = tFolder.newFile("sample.txt");
    assertTrue(txtFile.exists());
  }
} 

Every time the above test method is executed, a temporary folder is created and used in the test while executing and be deleted/garbage collected after the test run.

8. Junit: Can we pass command line arguments to a test execution?

Yes, We can pass command line arguments to a test execution by using -D JVM command-line options as shown below.

java -D JVM command-line options 

9. Explain Junit Test Fixture.

A test fixture refers a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.

Examples of fixtures are,

  • Loading a database with definite and sample data for testing.
  • Copying a definite known set of files for testing.
  • Preparing input data and setup/creation mock objects for test.
10. Can a Junit test method be declared as private?

No. Junit requires that all test methods must be declared as public.

Junit test method declared as private will compile without any issues however the execution will fail.

11. Junit: How do I test a protected method?

In Java, a protected method can only be accessed within the same package where the class is defined. So, define your Junit test class in the same package as the target class.

12. Junit: How do I test a private method?

The private methods cannot be tested as any private method only be accessed within the same class.

The private method need to tested manually or be converted to protected method.

13. Explain cyclomatic complexity.

Cyclomatic complexity is determined based on number of decision points within the code and hence execution paths. Higher the cyclomatic complexity makes it difficult to attain achieve test/code coverage.

14. What is JWebUnit?

JWebUnit is a Java-based testing framework for unit testing web applications. It wraps existing testing frameworks such as HtmlUnit and Selenium with a unified, simple testing interface to allow you to quickly test the correctness of your web applications.

15. List some of the JUnit extensions.

JUnit extensions include,

  • Cactus.
  • JWebUnit.
  • XMLUnit.
  • and MockObject.
16. What happens if a test method throws an exception?

The JUnit runner will declare that test as fail when the test method throws an exception.

17. What is Mockito?

Mockito is a popular mock framework that can be used in conjunction with JUnit. Mockito allows you to create and configure mock/dummy objects. Mockito greatly simplifies the development of tests for Java classes with external dependencies.

Mockito can be used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.

18. Mention a few mocking test frameworks.

Mockito and EasyMock.

19. What are the useful JUnit Extensions?
  • JWebUnit,
  • XMLUnit,
  • Cactus,
  • and MockObject.
«
»
Mockito Interview Questions

Comments & Discussions