1
public ClaimEntity create(ClaimEntity entity) {

    if (isBillEstimationActive) {
        return be_save(entity, BE_INSERT, new GeneratedKeyHolder());
    }

    return save(entity, INSERT, new GeneratedKeyHolder());
}

And the boolean variable is been set at the beginning of the class

private boolean isBillEstimationActive = false;

I want to call be_save and it won't be called until and unless it's the value of bool is true. How should I achieve it in my test case.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
ra_pri
  • 155
  • 2
  • 4
  • 20

2 Answers2

0

I guess you just need to set your private boolean field to true before to test it. If you have no other case like this you should use Java reflexion.

This is an example supposing your code is from ClaimRepository class :

ClaimRepository repository = new ClaimRepository();
Field field = ClaimEntity.getDeclaredField(isBillEstimationActive);
field.setAccessible(true);
field.set(repository, true);

You could also take a look to this answer : How do I test a private function or a class that has private methods, fields or inner classes?

Karbos 538
  • 2,977
  • 1
  • 23
  • 34
-1

You need to change that private variable's value by true.

for that you need to use PowerMock - WhiteBox class.

for example

Whitebox.setInternalState(classObject, “isBillEstimationActive”, true);

above statement while change the private variable's value.

for more information you can follow below link. WhiteBox.java

Bharat Vankar
  • 317
  • 2
  • 13