【问题标题】:Spring JpaRepository save() does not mock using MockitoSpring JpaRepository save() 不使用 Mockito 模拟
【发布时间】:2015-06-24 13:45:47
【问题描述】:

我是 Mockito 库的新手,但被困在某个地方。

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

// 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";
        }
    }

我写的测试用例是:

    @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) ;
        }

    }

如果我使用这样的东西

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

然后它可以工作并给我设备的非空对象。

这是什么原因?

【问题讨论】:

  • 然后它可以工作并给我设备的非空对象。还有什么问题?不是空对象?

标签: java spring mockito


【解决方案1】:

您将模拟设置为在收到给定的设备对象时返回一些内容:

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

这会告诉您的deviceDao 模拟在收到device 作为save 方法的参数时返回deviceEntity

Mockito 使用equals 进行参数匹配。这意味着如果您调用deviceDao.save(x),如果x.equals(device) 为true,则将返回deviceEntity

你的方法:

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";
}

这会在新的 Device 实例上调用 save()我非常怀疑这个 device 是否与您设置模拟的那个相同。

解决此问题的一种方法是在测试中使用更广泛的匹配器:

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

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

【讨论】:

  • @WesternGun OffsetDateTime 有一个有效且正常的equals() 方法。我建议您弄清楚为什么您没有收到您正在寻找的 OffsetDateTime 实例而不是使用 any()
  • 现在我看到日期时间问题不再存在并且无关紧要。但是不使用any(),当我告诉它返回创建的对象时,Mockito 只会返回我null。只有any(),我的jsonpath验证才会成功。奇怪。
  • @SalmanS when(device.save(x)).thenReturn(y) 只会在 .save() 上返回 y x 作为参数传递。模拟检查它是否确实是x 的方式是通过.equals()。如果x.equals(param) 为假,则不会触发模拟。您可以通过将.equals() 修复为Device 或使用不关心实际值的any(Device.class) 来解决此问题。我希望这能让我的答案不那么混乱。
  • @GuiSim 再问一个问题...在 TestCase 中自动连接存储库并使用 .save() 方法保存...当调用 ex 时。 userService.insertSome("ABC") 并且该存储库显示为空...请帮助
  • @SalmanS 对不起,我不明白。您可以复制并链接到存储库吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多