Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JpaRepository save() does not mock using Mockito

I am new to Mockito library and stuck somewhere.

Problem is when I mock save method of Spring jpaRepository I always get null. I am using such code in my project but for testing I have made a dummy code for testing. These are my code :

// This is the class for which I am making test case
    @Service("deviceManagementService")
    @Scope(BRASSConstants.SCOPE_SESSION)
    @Transactional
    public class DeviceManagementServiceImpl implements DeviceManagementService {

        public String  show(){
            Device device = new Device() ;
            device.setContactName("abc");
            Device deviceEntity = deviceDao.save(device);
            System.out.println(deviceEntity);  // i get this null always Why ???
            return "demo";
        }
    }

And the test case I am writing is :

    @RunWith(MockitoJUnitRunner.class)
    public class DemoTest {

        @InjectMocks
        private DeviceManagementServiceImpl deviceManagementServiceImpl;

        @Mock
        private DeviceDao deviceDao;

        @Before
        public void setUp() throws Exception {
            MockitoAnnotations.initMocks(this);
        }

        @Test
        public void show(){
            Device device = new Device() ;
            Device deviceEntity = new Device() ;
            deviceEntity.setDeviceId(12L);
            Mockito.when(deviceDao.save(device)).thenReturn(deviceEntity);

            Mockito.when(deviceManagementServiceImpl.show()).thenReturn(null) ;
        }

    }

If I use something like this

Mockito.when(deviceDao.findByDeviceSerialNo("234er")).thenReturn(deviceEntity); 

Then it works and give me not null object of Device.

What is the reason for this?

like image 511
Anand_5050 Avatar asked Jun 24 '15 13:06

Anand_5050


People also ask

How does JpaRepository save work?

The save operation performs a merge on the underlying EntityManager . This on its own doesn't perform any SQL but just returns a managed version of the entity. If that isn't already loaded into the EntityManager it might do this or it might identify the entity as a new one and make it a managed entity.

What does CrudRepository save return?

The save() method returns the saved entity, including the updated id field.


1 Answers

You setup your mock to return something when it receives a given device objet:

        Device device = new Device() ;
        Mockito.when(deviceDao.save(device)).thenReturn(deviceEntity);

This tells your deviceDao mock to return deviceEntity when it receives device as a parameter to the save method.

Mockito uses equals for argument matching. This means that if you call deviceDao.save(x), deviceEntity will be returned if x.equals(device) is true.

Your method:

public String  show(){
        Device device = new Device() ;
        device.setContactName("abc");
        Device deviceEntity = deviceDao.save(device);
        System.out.println(deviceEntity);  // i get this null always Why ???
        return "demo";
}

This calls save() on a new Device instance. I highly doubt that this device is equal to the one you setup your mock with.

One way to solve this is to use a broader matcher in your test:

Mockito.when(deviceDao.save(any(Device.class)).thenReturn(deviceEntity);

Or simply to ensure that the Device that you setup your mock with is the same as the one used in your code. I can't provide you with an example since your question does not include the code for Device.equals().

like image 93
GuiSim Avatar answered Oct 12 '22 14:10

GuiSim