Junit Interview Questions
Junit Interview Questions
Question: Why does JUnit only report the first failure in a single test?
Answer: 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.
Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 1/6 |
This page was exported from - TechnicalStack
Export date: Fri Jun 17 10:54:25 2022 / +0000 GMT
Question: What is Parameterized test in JUnit and what all annotations used for this?
Answer: Parameterized tests are possible in JUnit and they provides us the liberty of passing parameters into the test classes.
@RunWith(Parameterized.class) ? For making a class parametrized.
@Parameters ? Parameterized class must have a static method for generating and returning a collection of array, this method should
be marked with @Parameters annotation.
Question: When you should run JUnit test, Is there any particular time interval between each run?
Answer: No there is no time constraint. A JUnit test needs to run whenever there is a change in the source code. This ensures that the
new change passes through all the tests.
Question: Can we change return type of JUnit test method from void to some other type?
Answer: 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.
Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 2/6 |
This page was exported from - TechnicalStack
Export date: Fri Jun 17 10:54:25 2022 / +0000 GMT
Eclipse IDE comes with both Junit and it's plug-in for working with Junit.
Junit also has been ported to various other languages like PHP, Python, C++ etc.
Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 3/6 |
This page was exported from - TechnicalStack
Export date: Fri Jun 17 10:54:25 2022 / +0000 GMT
Each test runs in its own fixture so there can be no side effects among test runs.
Question14: The methods Get () and Set () should be tested for which conditions?
Answer: 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.
Question15: For which conditions, the methods Get () and Set () can be left out for testing?
Answer: You should do this test to check if a property has already been set (in the constructor) at the point you wish to call getX().
In this case you must test the constructor, and not the getX() method. This kind of test is especially useful if you have multiple
constructors.
Question16: 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. If you have two groups of tests that you think you'd like
to execute separately from one another, it is wise to place them in separate test classes.
Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 4/6 |
This page was exported from - TechnicalStack
Export date: Fri Jun 17 10:54:25 2022 / +0000 GMT
?protected?.
Question19: Do you need to write a main () method compulsorily in a Junit test case class?
Answer: No. But still developers write the main() method in a JUnit test case class to call a JUnit test runner to run all tests defined
in this class like:
public static void main(String[] args) {
junit.textui.TestRunner.run(Calculator.class);
}
Since you can call a JUnit runner to run a test case class as a system command, explicit main() for a Junit test case is not
recommended. junit.textui.TestRunner.run() method takes the test class name as its argument. This method automatically finds all
class methods whose name starts with test. Thus it will result in below mentioned findings:
testCreateLogFile()
testExists()
testGetChildList()
It will execute each of the 3 methods in unpredictable sequence (hence test case methods should be independent of each other) and
give the result in console.
Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 5/6 |
This page was exported from - TechnicalStack
Export date: Fri Jun 17 10:54:25 2022 / +0000 GMT
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4Cla
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassR
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoa
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4Class
at org.junit.internal.runners.CompositeRunner.runChildren(Compo
at org.junit.internal.runners.CompositeRunner.run(CompositeRunn
at org.junit.runner.JUnitCore.run(JUnitCore.java:130)
at org.junit.runner.JUnitCore.run(JUnitCore.java:109)
at org.junit.runner.JUnitCore.run(JUnitCore.java:100)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:81)
at org.junit.runner.JUnitCore.main(JUnitCore.java:44)
FAILURES!!!
Tests run: 1, Failures: 1
Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 6/6 |