JUnit Interview Questions

Advertisements


Dear readers, these JUnit Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of JUnit. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer:

Q: What is testing?

A: Testing is the process of checking the functionality of the application whether it is working as per requirements.

Q: What is unit testing?

A: Unit testing is the testing of single entity (class or method). Unit testing is very essential to every software company to give a quality product to their customers.

Q: What is Manual testing?

A: Executing the test cases manually without any tool support is known as manual testing.

Q: What is Automated testing?

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

Q: Out of Manual and Automated testing which one is better and why?

A: Manual Testing is:

  1. Time consuming and tedious.

  2. Huge investment in human resources.

  3. Less reliable.

  4. Non-programmable.

Automated testing is

  1. Fast

  2. Less investment in human resources

  3. More reliable

  4. Programmable

Q: What is JUnit?

A: JUnit is a unit testing framework for the Java Programming Language. It is written in Java and is an Open Source Software maintained by the JUnit.org community.

Q: What are important features of JUnit?

A: Import features of JUnit are:

  1. It is an open source framework.

  2. Provides Annotation to identify the test methods.

  3. Provides Assertions for testing expected results.

  4. Provides Test runners for running tests.

  5. JUnit tests can be run automatically and they check their own results and provide immediate feedback.

  6. JUnit tests can be organized into test suites containing test cases and even other test suites.

  7. JUnit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.

Q: What is a Unit Test Case?

A: A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected. 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 post condition.

Q: When are Unit Tests written in Development Cycle?

A: Tests are written before the code during development in order to help coders write the best code.

Q: Why not just use System.out.println() for testing?

A: Debugging the code using system.out.println() will lead to manual scanning of the whole output every time the program is run to ensure the code is doing the expected operations. Moreover, in the long run, it takes lesser time to code JUnit methods and test them on class files.

Q: How to install JUnit?

A: Follow the steps below:

  1. Download the latest version of JUnit, referred to below as junit.zip.

  2. Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%.

  3. Add JUnit to the classpath:

    set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%\junit.jar
    
  4. Test the installation by running the sample tests distributed with JUnit (sample tests are located in the installation directory directly, not the junit.jar file). Then simply type:

    java org.junit.runner.JUnitCore org.junit.tests.AllTests
    

    All the tests should pass with an "OK" message. If the tests don't pass, verify that junit.jar is in the CLASSPATH.

Q: Why does JUnit only report the first failure in a single test?

A: Reporting multiple failures in a single test is generally a sign that the test does too much and it is too big a unit test. JUnit is designed to work best with a number of small tests. It executes each test within a separate instance of the test class. It reports failure on each test.

Q: In Java, assert is a keyword. Won't this conflict with JUnit's assert() method?

A: JUnit 3.7 deprecated assert() and replaced it with assertTrue(), which works exactly the same way. JUnit 4 is compatible with the assert keyword. If you run with the -ea JVM switch, assertions that fail will be reported by JUnit.

Q: How do I test things that must be run in a J2EE container (e.g. servlets, EJBs)?

A: Refactoring J2EE components to delegate functionality to other objects that don't have to be run in a J2EE container will improve the design and testability of the software. Cactus is an open source JUnit extension that can be used for unit testing server-side java code.

Q: What are JUnit classes? List some of them.

A: JUnit classes are important classes which are used in writing and testing JUnits. Some of the important classes are:

  1. Assert - A set of assert methods.

  2. TestCase - It defines the fixture to run multiple tests.

  3. TestResult - It collects the results of executing a test case.

  4. TestSuite - It is a Composite of Tests.

Q: What are annotations and how are they useful in JUnit?

A: Annotations are like meta-tags that you can add to you code and apply them to methods or in class. The annotation in JUnit gives us information about test methods , which methods are going to run before & after test methods, which methods run before & after all the methods, which methods or class will be ignore during execution.

Q: How will you run JUnit from command window?

A: Follow the steps below:

  1. Set the CLASSPATH

  2. Invoke the runner:

    java org.junit.runner.JUnitCore 
    
Q: What is JUnitCore class?

A: JUnitCore is an inbuilt class in JUnit package. It is based on Facade design pattern. This class is used to run only specified test classes.

Q: What is Test suite?

A: Test suite means bundle a few unit test cases and run it together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.

Q: What is @Ignore annotation and how is this useful?

A: Sometimes it happens that our code is not ready and test case written to test that method/code will fail if run. The @Ignore annotation helps in this regards. Following are some of the usefulness of @Ignore annotation:

  1. You can easily identify all @Ignore annotations in the source code, while unannotated or commented out tests are not so simple to find.

  2. There are cases when you can't fix a code that is failing, but you still want to method to be around, precisely so that it does not get forgotten. In such cases @Ignore makes sense.

Q: What are Parameterized tests?

A: Parameterized tests allow developer to run the same test over and over again using different values.

Q: How do you use test fixtures?

A: Fixtures is 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. It includes:

  1. setUp() method which runs before every test invocation.

  2. tearDown() method which runs after every test method.

Q: How to compile a JUnit Test Class?

A: Compiling a JUnit test class is like compiling any other Java classes. The only thing you need watch out is that the JUnit JAR file must be included in the classpath.

Q: What happens if a JUnit Test Method is Declared as "private"?

A: If a JUnit test method is declared as "private", it compiles successfully. But the execution will fail. This is because JUnit requires that all test methods must be declared as "public".

Q: How do you test a "protected" method?

A: When a method is declared as "protected", it can only be accessed within the same package where the class is defined. Hence to test a "protected" method of a target class, define your test class in the same package as the target class.

Q: How do you test a "private" method?

A: When a method is declared as "private", it can only be accessed within the same class. So there is no way to test a "private" method of a target class from any test class. Hence you need to perform unit testing manually. Or you have to change your method from "private" to "protected".

Q: What happens if a JUnit test method is declared to return "String"?

A: If a JUnit test method is declared to return "String", the compilation will pass ok. But the execution will fail. This is because JUnit requires that all test methods must be declared to return "void".

Q: How can you use JUnit to test that the code throws desired exception?

A: JUnit provides a option of tracing the Exception handling of code. You can test if a code throws desired exception or not. The expected parameter is used along with @Test annotation as follows: @Test(expected)

Q: What are Parameterized tests in JUnit?

A: Parameterized tests allow developer to run the same test over and over again using different values.

Q: How to create Parameterized tests?

A: There are five steps, which you need to follow to create Parameterized tests:

  1. Annotate test class with @RunWith(Parameterized.class).

  2. Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.

  3. Create a public constructor that takes in what is equivalent to one "row" of test data.

  4. Create an instance variable for each "column" of test data.

  5. Create your tests case(s) using the instance variables as the source of the test data.

The test case will be invoked once per each row of data.

Q: Can you use a main() Method for Unit Testing?

A: Yes you can test using main() method. One obvious advantage seems to be that you can whitebox test the class. That is, you can test the internals of it (private methods for example). You can't do that with unit-tests. But primarily the test framework tests the interface and the behavior from the user's perspective.

Q: Do you need to write a test class for every class that needs to be tested?

A: No. We need not write an independent test class for every class that needs to be tested. If there is a small group of tests sharing a common test fixture, you may move those tests to a new test class.

Q: When are tests garbage collected?

A: The test runner holds strong references to all Test instances for the duration of the test execution. This means that for a very long test run with many Test instances, none of the tests may be garbage collected until the end of the entire test run. Explicitly setting an object to null in the tearDown() method, for example, allows it to be garbage collected before the end of the entire test run.

What is Next ?

Further you can go through your past assignments you have done with the subject and make sure you are able to speak confidently on them. If you are fresher then interviewer does not expect you will answer very complex questions, rather you have to make your basics concepts very strong.

Second it really doesn't matter much if you could not answer few questions but it matters that whatever you answered, you must have answered with confidence. So just feel confident during your interview. We at tutorialspoint wish you best luck to have a good interviewer and all the very best for your future endeavor. Cheers :-)



Advertisements
Advertisements