Search code examples
javaspringunit-testingjunitmockito

Alternative of whenNew in mockito for this usecase


Can some help me to mock AnotherClass object.

Things I have tried so far.

  1. MockConstruction
  2. useConstructor with settings
  3. When then statement

Powermockito we can not use for some reason.

ClassToBeTested {
  @Autowired
  DependencyOne dep1;
  @Autowired
  DependencyTwo dep2;

  method(){
    AnotherClass anotherClass = new AnotherClass(dep1,dep2);
anotherClass.someAction();
  }
}

Solution

  • I was able to handle this scenario by letting the call go to Another Class and then mocking the code present in some action() method with when then statements.