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.

     

 

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

1 month ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

2 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago