简体   繁体   English

Spring JpaRepository save()不使用Mockito进行模拟

[英]Spring JpaRepository save() does not mock using Mockito

I am new to Mockito library and stuck somewhere. 我是Mockito图书馆的新手并且卡在了某个地方。

Problem is when I mock save method of Spring jpaRepository I always get null. 问题是当我模拟Spring jpaRepository的save方法时,我总是得到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. 然后它工作,并给我不是Device的null对象。

What is the reason for this? 这是什么原因?

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. 这告诉你的deviceDao mock在接收device作为save方法的参数时返回deviceEntity

Mockito uses equals for argument matching. Mockito使用equals进行参数匹配。 This means that if you call deviceDao.save(x) , deviceEntity will be returned if x.equals(device) is true. 这意味着如果调用deviceDao.save(x) ,如果x.equals(device)为true,将返回deviceEntity

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. 这会在新的Device实例上调用save() I highly doubt that this device is equal to the one you setup your mock with. 我非常怀疑这个device与你设置模拟器的device相同。

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. 或者只是确保您设置模拟的Device与您的代码中使用的Device相同。 I can't provide you with an example since your question does not include the code for Device.equals() . 我无法向您提供示例,因为您的问题不包含Device.equals()的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM