无法在Junit中设置正确的期望。面对mockit.internal.UnexpectedInvocation

时间:2018-10-01 12:04:33

标签: java junit jmockit

下面是我的测试方法。

        @Test
            public void testSaveUserPreference() throws Exception {
                final long userId = 1L;
                final String category = "category";
                final UserPreference userPreference = createMockedUserPreference(userId, category);
                final UserPreferenceDTO userPreferenceDTO = createMockedUserPreferenceDTO(userId, category);
                final Date lastAccessed = userPreference.getLastAccessedDate();
                userPreferenceDTO.setLastAccessedDate(lastAccessed);
                //override the convert method to control the object being "saved"
                final UserPreference document = new UserPreference();
                new MockUp<UserPreferenceUtils>() {
                    @Mock
                    UserPreference convertFromDTO(UserPreferenceDTO dto) {
                        document.setUserId(userPreference.getUserId());
                        document.setCategory(userPreference.getCategory());
                        document.setProperties(userPreference.getProperties());
                        return document;
                    }
                };

                new Expectations() {{
                    component.getUserPreference(userId, category);
                    returns(userPreference);

                    component.saveUserPreference(userPreference);
                    returns(userPreference);
                }};

                UserPreferenceDTO actual = service.savePreference(userPreferenceDTO);
                assertNotNull(actual);
                assertEquals(userPreference.getUserId(), actual.getUserId());
                assertEquals(userPreference.getCategory(), actual.getCategory());
                assertEquals(userPreference.getProperties(), actual.getProperties());
                assertNotNull(actual.getCreatedDate());
                assertTrue(lastAccessed.before(actual.getLastAccessedDate()));
            }

下面是我遇到错误的服务方法。

            @Transactional
                public UserPreferenceDTO savePreference(UserPreferenceDTO userPreference) {
                    UserPreference preference = UserPreferenceUtils.convertFromDTO(userPreference);
                    UserPreference existingPreference = userPreferenceComponent.getUserPreference(userPreference.getUserId(), userPreference.getCategory());
                    if(existingPreference!=null && !CollectionUtils.isEmpty(existingPreference.getProperties())) {
                        Map<String,Object> report = (Map<String, Object>) preference.getProperties().get("favoriteFilters");
                        Map<String,Object> existingRep = (Map<String, Object>) existingPreference.getProperties().get("favoriteFilters");
                        existingRep.putAll(report);
                        existingPreference.getProperties().put("favoriteFilters",existingRep);
                    } else {
                        existingPreference = preference;
                    }
                    if (existingPreference.getCreatedDate() == null) {
                        existingPreference.setCreatedDate(new Date());
                    }
                    existingPreference.setLastAccessedDate(new Date());
                    UserPreferenceDTO savedPreference = UserPreferenceUtils.convertFromDocument(userPreferenceComponent.saveUserPreference(existingPreference));
                    return savedPreference;
                }

在第二行中调用save方法时,出现此错误。

mockit.internal.UnexpectedInvocation: Parameter "userPreference" of com.curaspan.platformsupportservice.components.preference.UserPreferenceComponent#saveUserPreference(com.curaspan.platformsupportservice.mongo.document.UserPreference userPreference) expected com.curaspan.platformsupportservice.mongo.document.UserPreference@3d3fcdb0, got com.curaspan.platformsupportservice.mongo.document.UserPreference@636be97c

期望和实际方法中的userPreference对象不匹配,尽管我所有参数都相同。有什么我想念的吗?

0 个答案:

没有答案