Skip to content
Advertisement

Is there way to mock pojo other then setting values manually in mockito?

consider a method returns class A in my service,

JavaScript

My pojo class

JavaScript

so in test case ,i wrote as

JavaScript

Here everything is working fine .Code coverage is also fine.But my question is ,is there any possible way to avoid above private method that returns class A by using mockito ?

Advertisement

Answer

in a unittest you define the behavior of your class depending on input. This means you have to specify this input. There is no way around it.

There are different ways to do that.

one way it to configure the DTO the way you did.

Another way is to generate the input data. But this has some downsites:
– you still need a method (or class) to do that,
– it makes your test harder to read and understand,
– it introduces failure possibilities in your test making it less reliable,

One more way is to set only those values needed for the current test in the //arrange section of your test method:

JavaScript
Advertisement