0

I want to unit test my service layer code using junit and mockito.Now in my service layer method instead of returning directly dao layer response,I am returning a wrapper response in following way

public MyResponseClass fetchMyEntityRecords(){

  List<MyEntity> myEntities = myDao.findAll();
  List<MyDto> myDtoList = new ArrayList<MyDto>();
  MyResponseClass obj = new MyResponseClass();
  if(myEntities!=null && !myEntities.isEmpty()){
     obj.setStatus("Success")
     obj.setMessage("Data Found");
     for(MyEntity myEntity : myEntities){
        MyDto myDto = new MyDto();
        BeanUtils.copyProperties(myEntity,myDto);
        myDtoList.add(myDto);
      }
      obj.setData(myDtoList)
   }else{
      obj.setStatus("Failure")
      obj.setMessage("Data Not Found");
      obj.setData(Collections.emptyList());
   }
      return obj;
 }

Now in Unit testing time

@Mock
private MyDao myDao;

@InjectMocks
private MyService myService;

@Test
void listOfMyEntityTest() {
    MyEntity myEntity1= new MyEntity();
    //setter method's invoked for setting value to enity1 object
     MyEntity myEntity2= new MyEntity();
    //setter method's invoked for setting value to enity2 object
    when(myDao.findAll()).thenReturn(List.of(enity1,enity2));
    var response = myService.fetchMyEntityRecords();
    Assertions.assertThat(response.getData().size()>0).isTrue();
}

I am getting following error

Expecting value to be true but was false 
org.opentest4j.AssertionFailedError: 
Expecting value to be true but was false

Now the response is actually MyResponseClass.Now my unit test is failing because i am unable to mock this MyResponseClass. How to achieve it?

Sumit Ghosh
  • 484
  • 4
  • 13
  • 36
  • 2
    Why do you want to mock MyResponseClass? Why is the test failing, what's the message? – Jonasz Jan 11 '23 at 18:09
  • 1
    Why should the test be failing if `MyResponseClass` is not mocked? Why does it require mocking? IMHO you should just return the real class here. – knittl Jan 11 '23 at 19:32
  • Where are enity1 and enity2 declared? – tgdavies Jan 12 '23 at 03:16
  • 1
    You *can't* mock `obj`, because you are creating it in the method you are testing. There doesn't seem any reason to need to mock it, but if you really need to you'll have to create a `MyResponseClassFactory` component and inject that. – tgdavies Jan 12 '23 at 03:36
  • @Jonasz I have posted the error log,in the post now you can find the assertion error there.It is my perception that because I am returning MyResponseClass in service layer method response so,I might have to mock it – Sumit Ghosh Jan 12 '23 at 07:33
  • In `obj.setData(myDtoList)` where does `myDtoList` come from? – tgdavies Jan 12 '23 at 07:34
  • @knittl you mean var keyword is causing issue? – Sumit Ghosh Jan 12 '23 at 07:35
  • @SumitGhosh what? No, I'm saying `MyResponseClass` does not need mocking. Where is `myDtoList` defined? It is not visible in your method. Is `myDao` in your SUT the correct one that's being set up in the test? – knittl Jan 12 '23 at 07:39
  • @knittl,@tgdavies, I have updated the post for myDtolist code,please check – Sumit Ghosh Jan 12 '23 at 07:40
  • Is that the actual code you're running? Otherwise the question just wastes everyone's time. – tgdavies Jan 12 '23 at 07:41
  • @tgdavies,yes actual code – Sumit Ghosh Jan 12 '23 at 07:41
  • 1
    Please step through `fetchMyEntityRecords` with the debugger when you run the test. Check that `myDao` is really the mock, and see why `response.getData()` is returning an empty `List`. – tgdavies Jan 12 '23 at 07:43
  • @SumitGhosh please use a debugger and check value of `myDao` and `myEntities` in your function. Also check if `myDao` is the same reference in your SUT and your test. (Might be [Why is my class not calling my mocked methods in unit test?](https://stackoverflow.com/q/74027324/112968) – if the wrong mock is injected, it is never set up and will return an empty list) – knittl Jan 12 '23 at 07:43

0 Answers0