Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:10:29+00:00 2026-06-16T00:10:29+00:00

Consider a class that uses an external jar. The class processes objects of type

  • 0

Consider a class that uses an external jar. The class processes objects of type D, which are obtained via objects A, B, and C, all of which external objects from the jar.

class DProcessor() {  
    public void process(PoolOfA pool) {
        A a = pool.borrowObject()
        ...
        B b = a.getB()
        C c = b.getC()
        for (D d : c.getAllDs()) {
            // Do something meaningful with d
        }
    }
}

How do I Unit test process(PoolOfA pool)?

My best shot so far is writing mocks for all external classes:

PoolOfA pool = mock(PoolOfA.class);

A a          = mock(A.class);
B b          = mock(B.class);
C c          = mock(C.class);

D d1         = mock(D.class);
D d2         = mock(D.class);
D d3         = mock(D.class);
D d4         = mock(D.class);

List listOfDs = new ArrayList<D>();
listOfDs.add(d1);
listOfDs.add(d2);
listOfDs.add(d3);
listOfDs.add(d4);

// Set specific behaviour for each d

when(pool.borrowObject()).thenReturn(a);
when(b.getC()).thenReturn(a);
when(c.getAllDs()).thenReturn(d);
when(b.getC()).thenReturn(c);
when(c.getAllDs()).thenReturn(listOfDs);

This seems cumbersome and inelegant. Is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:10:29+00:00Added an answer on June 16, 2026 at 12:10 am

    Better way is to rewrite the method, of course. But if you cannot do it for some reason, mockito offers great feature called ‘deep stubs’. Check out the docs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code that uses JSch to create an SSH connection: public class
Consider the following code sample that uses MEF to create an object of type
It may not be so obvious how respond_to? works in ruby. Consider that: class
I am building a class hierarchy that uses SSE intrinsics functions and thus some
I maintain an application that uses a (to me) surprising PHP quirk/bug/feature. Consider this
I have a container class that has a generic parameter which is constrained to
I have created a class that I've been using as the storage for all
The question Consider the string of digits 123456789 . Consider all arithmetic expression that
Consider the following sample code which uses a TrustManager to log whether an outgoing
Consider that I have 100 instances of a squeaker class. Each squeaker object is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.