DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Two Cool Java Frameworks You Probably Don’t Need
  • Mastering Unit Testing and Test-Driven Development in Java
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Improving Java Code Security

Trending

  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • The Future of Java and AI: Coding in 2025
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  1. DZone
  2. Culture and Methodologies
  3. Career Development
  4. Java Unit Testing Interview Questions

Java Unit Testing Interview Questions

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Aug. 06, 14 · Interview
Likes (3)
Comment
Save
Tweet
Share
48.0K Views

Join the DZone community and get the full member experience.

Join For Free

 The article presents some of the frequently asked interview questions in relation with unit testing with Java code. Please suggest other questions tthat you came across and I shall include in the list below.

  1. What is unit testing? Which unit testing framework did you use? What are some of the common Java unit testing frameworks?
    Ans: Read the definition of Unit testing on Wikipedia page for unit testing. Simply speaking, unit testing is about testing a block of code in isolation. There are two popular unit testing framework in Java named as Junit, TestNG.
  2. In SDLC, When is the right time to start writing unit tests?
    Ans: Test-along if not test-driven; Writing unit tests towards end is not very effective. Test-along technique recommends developers to write the unit tests as they go with their development.
  3. With Junit 4, do we still need methods such as setUp and tearDown?
    Ans: No. This is taken care with help of @Before and @After annotations respectively
  4. What do following junit test annotations mean?
    Ans: Following is a list of frequently used JUnit 4 annotations:@Test (@Test identifies a test method)
    @Before (Ans: @Before method will execute before every JUnit4 test)@After (Ans: @After method will execute after every JUnit4 test)@BeforeClass (Ans: @BeforeClass method will be executed before JUnit test for a Class starts)@AfterClass (Ans: @AfterClass method will be executed after JUnit test for a Class is completed)@Ignore (@Ignore method will not be executed)
  5. How do one do exception handling unit tests using @Test annotation?
    Ans: @Test(expected={exception class}. For example: @Test(expected=IllegalArgumentException.class)
  6. Write a sample unit testing method for testing exception named as IndexOutOfBoundsException when working with ArrayList?

    @Test(expected=IndexOutOfBoundsException.class)
    public void outOfBounds() {
    new ArrayList<Object>().get(1);
    }
  7. Write a sample unit testing method for testing timeout?

    @Test(timeout=100)
    public void infinity() {
    while(true);
    }
  8. What is unit testing method naming convention that you follow?
    Ans: Unit tests name should act like the documentation giving a clear idea on what functionality is tested. There are various techniques that could be used to name unit tests. Some of them are following: Given…When…Then.. or Should…Then… etc.
  9. What are called as test smells in relation with unit testing?
    Ans: Following could be termed as test smells: Multiple assertions within one unit test, long-running unit tests etc
  10. What are top 2-3 advantages of writing unit tests?
    Ans: Design testability, Code testability, Code maintainability as good unit tests enforces OO principles such as Single Responsitbility etc which enables people avoid code smells such as long classes, long methods, large conditionals etc.
  11. What are top 2-3 characteristics of Hard-to-test code?
    Ans: Long methods, Long conditionals, mixing concerns, bulky constructors
  12. What is mocking & stubbing? What are key differences between them? Did you use any mocking framework?
    Ans: Read details on What is difference between stubbing and mocking on this page.. There are various different Java mocking framework such as Mockito, EasyMock etc.
  13. What are top 2-3 advantages of mocking?
    Ans: 1. Verify interactions 2. Test the block of code in isolation
  14. How is code cyclomatic complexity related to unit tests?
    Ans: As code cyclomatic complexity is determined based on number of decision points within the code and hence execution paths, higher cyclomatic complexity makes it difficult to attain achieve test/code coverage.
  15. What is code coverage? How is it measured? Name some JUnit code coverage tools?Ans: Cobertura, EclEmma
  16. Name some code smells which makes it hard to achieve high code coverage?
    Ans: Long method, Large conditionals
  17. What is test-driven development (TDD)?
    Ans: Read the details on Wikipedia page for TDD.
unit test Java (programming language) Interview (journalism) Cyclomatic Complexity

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Two Cool Java Frameworks You Probably Don’t Need
  • Mastering Unit Testing and Test-Driven Development in Java
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Improving Java Code Security

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!