0% found this document useful (0 votes)
2 views4 pages

15 Junit Interview Questions II

The document contains a list of 15 JUnit interview questions along with their answers, covering topics such as testing methods with different access modifiers, the use of JUnitCore, and the purpose of test suites. It emphasizes best practices in unit testing, including when to create tests and how often they should be run. Additionally, it provides insights into the structure of JUnit test classes and the functionality of assertions and verifications.

Uploaded by

subbareddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

15 Junit Interview Questions II

The document contains a list of 15 JUnit interview questions along with their answers, covering topics such as testing methods with different access modifiers, the use of JUnitCore, and the purpose of test suites. It emphasizes best practices in unit testing, including when to create tests and how often they should be run. Additionally, it provides insights into the structure of JUnit test classes and the functionality of assertions and verifications.

Uploaded by

subbareddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

18/07/2025, 11:22 15 Junit Interview questions II

« Testing / Junit Interview questions II »

1. Can we use system.out.println() along with Unit test method?

2. Should we create test cases for getters and setters of a bean?

3. How will you test a method with protected access modifier?

4. How will you test a method with private access modifier?

5. Should every Junit test Class with in suite should have main() method?

6. What is JUnitCore class?

7. Differentiate Assert and Verify.

8. How @Test annotation is used for testing exceptions?

9. When Unit Tests are created during software life cycle?

10. Procedure to create a simple Junit test class.

11. What Is Junit TestSuite?

12. Can a test method be declared as private or protected?

13. Can a test method return a value?

14. What happens when a test method throws an exception?

15. How often should you run your JUnit Tests?

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

1. Can we use system.out.println() along with Unit test method?

Debugging the code using sysout requires manual scanning and it is not recommended.

2. Should we create test cases for getters and setters of a bean?

Not required. most of the getters and setters are less like to break. However we must consider adding test cases of
getters/setters involving complex data types or special logic.

3. How will you test a method with protected access modifier?

Define your Junit test class in the same package as that of class that has the target protected method.

https://www.javapedia.net/Junit-Interview-questions-II 1/4
18/07/2025, 11:22 15 Junit Interview questions II

4. How will you test a method with private access modifier?

There is no way to test it as the private method can not be accessed outside of the class. Manual testing may be
performed or use of Reflection API could help. Also consider changing the access modifier to protected.

5. Should every Junit test Class with in suite should have main() method?

Not required. However we may add main() method to run only the tests from that Test class.

public static void main(String[] args) {


junit.textui.TestRunner.run(MyTestClass.class);
}

6. What is JUnitCore class?

org.junit.runner.JUnitCore class is responsible for executing tests. runClasses() method of JUnitCore class enables
running the one or more test classes which yield Result Object (org.junit.runner.Result).

The test results will be extracted from the Result Object.

JUnitCore is based on facade design pattern.

7. Differentiate Assert and Verify.

Assert works only if assertions ( -ea ) are enabled which is not required for Verify.

Assert throws an exception and hence it discontinue abruptly with the test if assert evaluates to false whereas it's
not so with Verify.

8. How @Test annotation is used for testing exceptions?

@Test (expected = Exception.class)

The limitation is only one exception per test method.

9. When Unit Tests are created during software life cycle?

During development cycle, developers create unit tests for the functionality they are developing. It is developed
parallelly along with actual code or immediately after the actual implementation.

10. Procedure to create a simple Junit test class.

Junit3

https://www.javapedia.net/Junit-Interview-questions-II 2/4
18/07/2025, 11:22 15 Junit Interview questions II

1.Define a subclass of TestCase.

2.Override the setUp() method to initialize object(s) under test.

3.Optionally override the tearDown() method to release object(s) under test.

4.Define one or more public testXYZ() methods that exercise the object(s) under test and assert expected results.

11. What Is Junit TestSuite?

junit.framework.TestSuite is a container class that allows grouping and organizing multiple test cases into a
collection and run them together.

12. Can a test method be declared as private or protected?

No. The Test class will compile however it will not be executed.

13. Can a test method return a value?

For e.g. if the test method returns a List of Objects or an primitive datatype, it will not be excuted as a test method
even though it compiles.

14. What happens when a test method throws an exception?

The JUnit runner declares that test as failed.

15. How often should you run your JUnit Tests?

You should run all your unit tests as often as possible, ideally every time the code is changed. Make sure all your
unit tests always run at 100%. Frequent testing gives you confidence that your changes didn't break anything and
generally lowers the stress of programming in the dark.

« Junit Interview questions III »

Comments & Discussions

https://www.javapedia.net/Junit-Interview-questions-II 3/4
18/07/2025, 11:22 15 Junit Interview questions II

0 Comments 
1 Login

G Start the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

 Share Best Newest Oldest

Be the first to comment.

Subscribe Privacy Do Not Sell My Data

Interviews Questions

Java
Spring
Hibernate
Maven About Javapedia.net
Testing
Javapedia.net is for Java and J2EE developers, technologist and
API college students who prepare of interview. Also this site includes
BigData many practical examples.
Web
DataStructures This site is developed using J2EE technologies by Steve Antony, a
senior Developer/lead at one of the logistics based company.
Database
MuleESB
Cloud
Scala
Tools

contact: javatutorials2016[at]gmail[dot]com

Kindly consider donating for maintaining this website. Thanks.

Copyright © 2020, javapedia.net, all rights reserved. privacy policy.

https://www.javapedia.net/Junit-Interview-questions-II 4/4

You might also like