Devops JUnit Interview Questions and Answers Pdf
1. What is JUnit?
Answer: 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.
2. What are important features of JUnit?
Answer: Import features of JUnit are:
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 the test is going fine and it turns red when a test fails.
3. What is a Unit Test Case?
Answer: A Unit Test Case is a part of code which ensures that 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 postcondition.
4. Why does JUnit only report the first failure in a single test?
Answer: 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.
5. In Java, assert is a keyword. Won’t this conflict with JUnit’sassert() method?
Answer: 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.
6. How do I test things that must be run in a J2EE container (e.g. servlets, EJBs)?
Answer: 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.
7. What are JUnit classes? List some of them?
Answer: JUnit classes are important classes which are used in writing and testing units. Some of the important classes are:
Assert – A set of assert methods.
Test Case – It defines the fixture to run multiple tests.
Test Result – It collects the results of executing a test case.
Test Suite – It is a Composite of Tests.
8. What are annotations and how are they useful in JUnit?
Answer: Annotations are like meta-tags that you can add to your 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 ignored during execution.
9. What Is JunitTestCase?
Answer: JUnit Test Case is the base class, JUnit. framework.TestCase, that allows you to create a test case. (Although, TestCase class is no longer supported in JUnit 4.4.)
A test case defines the fixture to run multiple tests. To define a test case
Implement a subclass of TestCase
Define instance variables that store the state of the fixture
Initialize the fixture state by overriding setUp
Clean-up after a test by overriding tearDown
Each test runs in its own fixture so there can be no side effects among test runs.
10. What is Junit Test Fixture?
Answer: A test fixture is a fixed state of a set of objects used as a baseline for running tests. Their purpose 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:
Loading a database with a specific, known set of data
Copying a specific known set of files
Preparation of input data and setup/creation of fake or mock objects
If a group of tests shares the same fixtures, you should write a separate setup code to create the common test fixture. If a group of tests requires different test fixtures, you can write code inside the test method to create its own test fixture.
11. Explain how you can write a simple JUnit test case?
Answer:
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.
12. Mention what are parameterized tests? Answer: Parameterized tests enable the developer to perform the same test over and again using different values.
13. Different methods of exception handling in JUnit?
Answer: There are different methods of exception handling in JUnit
Try catch idiom
With JUnit rule
With @Test annotation
With catch exception library
With customs annotation
14. What is ignore test in JUnit?
Answer: 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
15. List out some useful JUnit extensions?
Answer: JUnit extensions include
Cactus
JWebUnit
XMLUnit
MockObject
16. Explain who should use JUnit – a developer or tester? Why you use JUnit to test your code?
Answer: JUnit is more often used by developers to implement unit tests in JAVA. It is designed for unit testing that is more of a coding process and not a testing process. However, many testers and QA engineers use JUnit for unit testing.
JUnit is used because
It tests early and does automate testing
JUnit tests can be compiled with the build so that at the 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
17. Explain what is JUnitCore Class?
Answer: 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.
18. Explain how you can run JUnit from the command window?
Answer: To run JUnit from the command window, you have to follow the steps
Set the CLASSPATH
Invoke the runner:
java org.junit.runner.JUnitCore
19. Name the tools with which JUnit can be easily integrated?
Answer: JUnit Framework can be easily integrated with either of the followings:
Eclipse
Ant
Maven
20. What are the core features of JUnit?
Answer: JUnit test framework provides the following important features
- Fixtures
- Test suites
- Test runners
- JUnit classes
21. What is a fixture?
Answer: 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 the following methods.
setUp() method which runs before every test invocation.
tearDown() method which runs after every test method.
22. What is a test suite?
Answer: Test suite means bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.
23. What is a test runner?
Answer: Test runner is used for executing the test cases.
24. Explain the execution procedure of the JUnit test API methods?
Answer: Following is how the JUnit execution procedure works:
First of all, a method annotated as @BeforeClass execute only once.
Lastly, the method annotated as @AfterClass executes only once.
The method annotated as @Before executes for each test case but before executing the test case.
The method annotated as @After executes for each test case but after the execution of the test case.
In between method annotated as @Before and method annotated as @After each test case executes.
25. What is the purpose of org.junit.JUnitCore class?
Answer: The test cases are executed using JUnitCore class. JUnitCore is a facade for running tests. It supports running JUnit 4 tests, JUnit 3.8.x tests, and mixtures.
26. How to simulate the timeout situation in JUnit?
Answer: Junit provides a handy option for Timeout. If a test case takes more time than the specified number of milliseconds then Junit will automatically mark it as failed. The timeout parameter is used along with @Test annotation.
27. What are the Parameterized tests?
Answer: Junit 4 has introduced a new feature Parameterized tests. Parameterized tests allow the developer to run the same test over and over again using different values.
28. How to Compile a JUnit Test Class?
Answer: Compiling a JUnit test class is like compiling any other Java classes. The only thing you need to watch out is that the JUnit JAR file must be included in the classpath.
29. What happens if a JUnit Test Method is Declared as “private”?
Answer: 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”.
30. How do you test a “protected” method?
Answer: 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.
31. How do you test a “private” method?
Answer: 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”.
32. What happens if a JUnit test method is declared to return “String”?
Answer: 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”.
33. Can you use a main() Method for Unit Testing?
Answer: 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.
34. Do you need to write a test class for every class that needs to be tested?
Answer: 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.
35. When are tests garbage collected?
Answer: 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.
36. What is a Mock Object?
Answer: In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test.
37. Explain unit testing using Mock Objects?
Answer: The common coding style for testing with mock objects is to −
Create instances of mock objects.
Set state and expectations in the mock objects.
Invoke domain code with mock objects as parameters.
Verify consistency in the mock objects.
38. What is Cactus?
Answer: Cactus is a simple test framework for unit testing server-side java code (Servlets, EJBs, Tag Libs, Filters). The intent of Cactus is to lower the cost of writing tests for server-side code. It uses JUnit and extends it. Cactus implements an in-container strategy, meaning that tests are executed inside the container.
39. What is JWebUnit?
Answer: 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.
40. What are the advantages of using JWebUnit?
Answer: 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.
Note: Browse latest Devops Interview Questions and Devops training videos. Here you can check Devops Online Training details and Devops Training Videos for self learning. Contact +91 988 502 2027 for more information.