Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

JUnit Interview Questions and Answers

by Mohammed, on Mar 15, 2018 2:46:27 PM

JUnit Interview Questions and Answers

Q1. What is JUnit?

Ans: JUnit is a testing framework for unit testing.  It uses Java as a programming platform, and it is an Open Source Software managed by the JUnit.org community.

Q2. What is Unit Test Case?

Ans: Unit Test Case is a part of the code that ensures that the another part of code (method) behaves as expected. For each requirement, there must be at least two test cases one negative test and one positive test.

Q3. List important features of JUnit?

Ans: The important features of JUnit:

  • It is an open source framework.
  • Provides Annotation to identify the test methods.
  • Provides Assertions for testing expected results.
  • Provides Test runners for running tests.
  • JUnit tests can be run automatically and they check their own results and provide immediate feedback.
  • JUnit tests can be organized into test suites containing test cases and even other test suites.
  • JUnit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.

Q4. How to install JUnit?

Ans: Installation steps for JUnit :

  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
  • 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.

Q5.  How you can write a simple JUnit test case?

Ans:

  • Determine a subclass of TestCase
  • To initialize object(s) under test, override the setup() method
  • To release object(s) under test override the teardown() method

Determine one or more public test XYZ() methods that exercise the objects under test and assert expected results.

Q6. What are parameterized tests?

Ans: Parameterized tests enable developer to perform the same test over and again using different values.

Q7. What is the difference between JUnit and TestNG?

Ans:

                                JUnit                                    TestNG
  • In JUnit the naming convention for annotation is a bit complicated for, e.g., “Before”, “After” and “Expected”
  • In JUnit, for a method declaration you have to follow a specific style like using “@BeforeClass” and “@AfterClass”.
  • In JUnit method name constraint is present
  • JUnit framework does not have “Parameterized Test” or “Dependency Test” feature
  • In JUnit grouping of test cases are not available
  • JUnit does not support parallel execution on Selenium test cases
  • It cannot re-run the failed cases
  •  In TestNG it is easier to understand annotations like “BeforMethod”, “AfterMethod” and “ExpectedException”
  • In TestNG, there is no restriction like you have to declare methods in a specific format
  • In TestNG method name constraint is not present, and you can determine any test method names
  • TestNG use “dependOnMethods” to implement the dependency testing
  • In TestNG, grouping of test cases is available
  • In TestNG Parallel execution of Selenium test cases are possible
  • It can rerun the failed tests

 

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

Ans: Different methods of exception handling in JUnit are:

  • Try catch idiom
  • With JUnit rule
  • With @Test annotation
  • With catch exception library
  • With customs annotation
 

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

 

Ans: 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.

Q10.  What is ignore test in JUnit?

Ans: When your code is not ready, and it would fail if executed then you can use @Ignore annotation.

  • It will not execute a test method annotated with @Ignore
  • It will not execute any of the test methods of test class if it is annotated with @Ignore

Q11.  Mention  some useful JUnit extensions?

Ans: JUnit extensions include:

  • Cactus
  • JWebUnit
  • XMLUnit
  • MockObject

Q12. Explain who should use JUnit – a developer or tester? Why you use JUnit to test your code?

Ans: JUnit is more often used by developers to implement unit tests in JAVA.  It is designed for unit testing that is more a coding process and not a testing process. However, many testers and QA engineers use JUnit for unit testing.

JUnit is used because:

  • It test early and does automate testing
  • JUnit tests can be compiled with the build so that at unit level, regression testing can be done
  • It allows test code re-usage
  • JUnit tests behave as a document for the unit tests when there is a transfer

Q13. What is JUnitCore Class?

Ans: JUnitCore class is an inbuilt class in JUnit package; it is based on Façade design pattern, this class is used to run only definite test classes only.

Q14. Explain the execution procedure of the JUint test API methods?

 

Ans: Following is how the JUnit execution procedure works:

  • First of all method annotated as @BeforeClass execute only once.
  • Lastly, the method annotated as @AfterClass executes only once.
  • Method annotated as @Before executes for each test case but before executing the test case.
  • Method annotated as @After executes for each test case but after the execution of test case.
  • In between method annotated as @Before and method annotated as @After each test case executes.

Q15.   How you can run JUnit from the command window?

Ans: To run JUnit from the command window, you have to follow the steps:

  • Set the CLASSPATH
  • Invoke the runner

Q16. What are JUnit Classes? List some of them.

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

  • Assert: It contains a set of assert methods.
  • TestCase: It contains a test case defines the fixture to run multiple tests.
  • TestResult: It contains methods to collect the results of executing a test case.
  • TestSuite: It is a Composite of Tests.

Q16. What are annotations and how are they useful in JUnit?

Ans: 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.

Q17. How will you run JUnit from command window?

Ans: Follow the steps below:

  • Set the CLASSPATH
  • Invoke the runner
  • java org.junit.runner.JUnitCore

Q18. What are the disadvantages of manual testing?

The disadvantages of manual testing are as follows:

  • Time consuming: As test cases are executed by human resources so it is very slow and tedious.
  • Huge investment in human resources: As test cases need to be executed manually so more analyzers are required in manual testing.
  • Less reliable: It is less reliable as tests may not be performed with exactness each time because of human errors.
  • Non-programmable: No programming can be done to write complex tests which get concealed data.

Q19. What are the advantages of automated testing?

Ans:

The advantages of automated testing are as follows:

  • Fast: Automation testing process is significantly faster than human resources.
  • Less investment in human resources: Test cases are executed by using automation tool so less tester are required in automation testing.
  • More reliable: Automation tests implement precisely same operation every time they are run.
  • Programmable: Testers can program sophisticated tests to get the hidden information.

Q20. Name the tools with which JUnit can be easily integrated.

Ans: JUnit Framework can be easily integrated with either of the followings:

  • Eclipse
  • Ant
  • Maven.

Q21. What is a fixture?

Ans: Fixture 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 following methods:

  • setUp() method which runs before every test invocation.
  • tearDown() method which runs after every test method.

Q22. What is a test suite?

Ans: 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.

Q23. What is a test runner?

Ans: Test runner is used for executing the test cases.

Q24. What is the purpose of org.junit.TestResult class?

Ans: A TestResult collects the results of executing a test case. It is an instance of the Collecting Parameter pattern. The test framework distinguishes between failures and errors. A failure is anticipated and checked for with assertions. Errors are unanticipated problems like an ArrayIndexOutOfBoundsException.

 

Q25. What is JWebUnit?

Ans: WebUnit is a Java-based testing framework for 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.

 

Q26. What are the advantages of using JWebUnit?

Ans: JWebUnit provides a high-level Java API for navigating a web application combined with a set of assertions to verify the application's correctness. This includes navigation via links, form entry and submission, validation of table contents, and other typical business web application features.

The simple navigation methods and ready-to-use assertions allow for more rapid test creation than using only JUnit or HtmlUnit. And if you want to switch from HtmlUnit to other plugins such as Selenium (available soon), there is no need to rewrite your tests.

Q27. Why does JUnit only report the first failure in a single test?

Ans: There is no particular answer for this question, the main reason it does it like this to make the testing process simple and easy. Even though the failures are more than one it only reports the first failure, resolving which may implicitly resolve other unreported failures too.

Q28. Is it possible to pass command-line arguments to a test execution?

Ans: Yes, It’s possible to pass command line arguments to a test execution.

You should use this command:

-D JVM command-line options.

Q29. Can we change return type of JUnit test method from void to some other type?

Ans: Ideally you should not do this. All the JUnit test methods should have a void return type. If you change the return type then the test method would not be considered as a test method and would be ignored during execution of tests.

Q30. The methods Get () and Set () should be tested for which conditions?

Ans: Unit tests performed on java code should be designed to target areas that might break. Since the set() and get() methods on simple data types are unlikely to break, there is no need to test them explicitly. On the other hand, set() and get() methods on complex data types are vulnerable to break. So they should be tested.

Topics:JUnit Interview Questions And AnswersInformation Technologies (IT)

Comments

Subscribe

Top Courses in Python

Top Courses in Python

We help you to choose the right Python career Path at myTectra. Here are the top courses in Python one can select. Learn More →

aathirai cut mango pickle

More...