Search code examples
javajunitmockingpowermock

Mocked repository becoming null in service


Hi I have a springboot application which has a service in which repository is mocked in test case but it is not getting mocked.Below is my code sample

@Service("HRfeedService")
public class HRfeedService {
 @Autowired
    private HRfeedRepository hRfeedRepository;
 public boolean getBBHRFeedDetails(String sid){

        
       List<HRfeed> list = hRfeedRepository.getHRfeedDetailsBySID(sid);
       
        if(list.size()>0){
          
            bbHrStatus = true;
        }

        return bbHrStatus;
    }
}

My repository class here:

@Repository
public interface HRfeedRepository extends JpaRepository<HRfeed,HRfeedID> {

    @Query(value = "select * from HRFEED_Details hr where hr.sid= ?1 and ACTIVE_FLAG='1'",nativeQuery = true)
    public List<HRfeed> getHRfeedDetailsBySID(String sid);

}

Mytest clas is here:

@RunWith(SpringJUnit4ClassRunner.class)
public class TestHRfeedService2 {

    String sid="A123456";
    @InjectMocks
    private HRfeedService hRfeedService;
    @Mock
    HRfeedRepository hRfeedRepository;
 @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        hRfeedService = new HRfeedService();
    }
 @Test
    public void testGetBBHRFeedDetails() throws Exception
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy hh.mm.ss.SSSSSSSSS a");
        ArrayList<HRfeed> list = new ArrayList<>();
        HRfeed hRfeed = new HRfeed();
        hRfeed.setSid("A123456");
        hRfeed.setEmail("[email protected]");
        hRfeed.setACTIVE_FLAG('1');
        Timestamp timestamp = new Timestamp(dateFormat.parse("19-NOV-19 04.43.17.740000000 PM").getTime());
        hRfeed.setEFF_UNTIL_DATE(timestamp);
        hRfeed.setEFF_ASOF_DATE(timestamp);
        list.add(hRfeed);
        Mockito.when(hRfeedRepository.getHRfeedDetailsBySID(anyString())).thenReturn(list);

        boolean result = hRfeedService.getBBHRFeedDetails(sid);
        assertEquals(result,true);

    }
}

I would like to use powermockrunner if possible otherwise also fine.When I am mocking repository it is not getting mocked instead in service it is taken as null.

Please let me know how to mock my service.


Solution

  • It looks to me like you have forgotten to inject the repository into the service in the test.

    In "production" Spring will handle this for you, but in the test you are using Mockito. You have a couple of choices, one is to use Spring's test infrastructure, the other is to provide a constructor on the service that takes an instance of the Repository.

    Something like:

    @Service("HRfeedService")
    public class HRfeedService {
    
       ... rest of class
    
       @Autowired
       private HRfeedRepository hRfeedRepository;
    
       public HRfeedService(HRfeedRepository hRfeedRepository) {
          this.hRfeedRepository = hRfeedRepository;
       }
    
       ... rest of class
    }
    

    In the @Before of the test class something like:

       hRfeedService = new HRfeedService(mockRepository);
    

    where mockRepository if the repository mock you have set up with Mockito.

    Personally I lean towards using constructors for autowiring rather than fields, this allows me to make my fields final, and often I can omit the @Autowired as Spring can figure it out.