0

Is there a way to mock Class instance in java

  @Autowired
  private ApplicationContext context;

 public <T> T search(Class<? extends JpaSpecificationExecutor<ENTITY>> 
specExecutor,
  Class<? extends Converter<DTO, ENTITY>> objConverter) {
 JpaSpecificationExecutor jpaSpecificationExecutor = context.getBean(specExecutor);
 Converter converter = context.getBean(objConverter);
}

I have to unit test the above method, i am using Mockito.

The issue is, when i use:

  Mockito.when(context.getBean(any(Class.class))).thenReturn(new MyJpaSpecificationExecutor());

The above will not work for context.getBean(objConverter); // ClassCastException

Is there a way in Mockito or PowerMockito to achieve this?

Ritesh Kaushik
  • 715
  • 2
  • 13
  • 24

0 Answers0