0

I have something as,

classB { classA a = new classA(); ... } classA { ... } 

I would like to write junit on classB. How to mock object a of classA?

2
  • 1
    One should not create A within B's constructor and instead use Inversion of Control to pass an instance of A to B's constructor. The only other way that comes to my mind is using powermock, but this messes with the bytecode and thus cann screw up JaCoCo Code coverage. Commented Jul 25, 2020 at 11:50
  • Mockito can mock object construction since 3.5.0: javadoc.io/static/org.mockito/mockito-core/5.14.2/org/mockito/… Commented Nov 29, 2024 at 17:30

1 Answer 1

0

Pass the A in as a parameter to the constructor of B.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.