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?

Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 1
    One should not create `A` within `B`'s constructor and instead use [Inversion of Control](https://en.wikipedia.org/wiki/Inversion_of_control#:~:text=In%20software%20engineering%2C%20inversion%20of,control%20from%20a%20generic%20framework.) 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. – Turing85 Jul 25 '20 at 11:50

1 Answers1

0

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347