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
Spring JpaRepository save() does not mock using Mockito
提问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 deviceDao
mock to return deviceEntity
when it receives device
as a parameter to the save
method.
这告诉你的deviceDao
模拟deviceEntity
在它device
作为save
方法的参数接收时返回。
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.
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 Device
instance.
I highly doubt that this device
is 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 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()
.
或者只是为了确保Device
您设置的模拟与您的代码中使用的相同。我无法为您提供示例,因为您的问题不包含Device.equals()
.