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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31028359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:57:50  来源:igfitidea点击:

Spring JpaRepository save() does not mock using Mockito

javaspringmockito

提问by Anand_5050

I am new to Mockito library and stuck somewhere.

我是 Mockito 图书馆的新手并被困在某个地方。

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 :

问题是当我模拟 Spring jpaRepository 的保存方法时,我总是得到空值。我在我的项目中使用了这样的代码,但为了测试,我制作了一个虚拟代码进行测试。这些是我的代码:

// 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?

这是什么原因?

回答by GuiSim

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 deviceDaomock to return deviceEntitywhen it receives deviceas a parameter to the savemethod.

这告诉你的deviceDao模拟deviceEntity在它device作为save方法的参数接收时返回。

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

Mockitoequals用于参数匹配。这意味着如果您调用deviceDao.save(x),deviceEntity将返回如果x.equals(device)为真。

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 Deviceinstance. I highly doubt that this deviceis equal to the one you setup your mock with.

这会调用save()一个新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 Devicethat 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().

或者只是为了确保Device您设置的模拟与您的代码中使用的相同。我无法为您提供示例,因为您的问题不包含Device.equals().