使用Mockito模拟出Spring Boot存储库删除调用会在get()

时间:2018-11-21 09:00:38

标签: spring-boot mockito

我是Spring Boot和Mockito的新手,在服务测试中模拟存储库调用时遇到问题。

我有一个“删除”服务方法调用,如下所示,我试图通过模拟存储库调用来对Mockito进行测试:

public interface IEntityTypeService {
    public EntityType getById(long id);
    public EntityType getByName(String name);
    public List<EntityType> getAll();
    public void update(EntityType entityType);
    public void delete(long id);
    public boolean add(EntityType entityType);
}

@Service
public class EntityTypeServiceImpl implements IEntityTypeService {
    @Autowired
    private EntityTypeRepository entityTypeRepository;

    @Override
    public void delete(long id) {
        entityTypeRepository.delete(getById(id));   
    }

    @Override
    public EntityType getById(long id) {
        return entityTypeRepository.findById(id).get();
    }

....implementation of other methods from the interface
}

我的存储库如下:

@RepositoryRestResource
public interface EntityTypeRepository extends LookupObjectRepository<EntityType> {

}

我没有实现存储库中的任何方法,因为我让Spring Boot为我连接它。

我的测试如下:

@RunWith(SpringRunner.class)
public class EntityTypeServiceTest {
    @TestConfiguration
    static class EntityTypeServiceImplTestContextConfiguration {

        @Bean
        public IEntityTypeService entityTypeService() {
            return new EntityTypeServiceImpl();
        }
    }

    @Autowired
    private IEntityTypeService entityTypeService;
    @MockBean
    private EntityTypeRepository entityTypeRepository;

    @Test
    public void whenDelete_thenObjectShouldBeDeleted() {                
        final EntityType entity = new EntityType(1L, "new OET");
        Mockito.when(entityTypeRepository.findById(1L).get()).thenReturn(entity).thenReturn(null);

        // when
        entityTypeService.delete(entity.getID());

        // then
        Mockito.verify(entityTypeRepository, times(1)).delete(entity);
        assertThat(entityTypeRepository.findById(1L).get()).isNull();       
    }
}

运行测试时,出现错误消息“ java.util.NoSuchElementException:无值”

java.util.NoSuchElementException: No value present
    at java.util.Optional.get(Optional.java:135)
    at xyz.unittests.service.EntityTypeServiceTest.whenDelete_thenObjectShouldBeDeleted(OriginatingEntityTypeServiceTest.java:41)

它引用了测试中的行Mockito.when(originatingEntityTypeRepository.findById(1L).get()).thenReturn(entity).thenReturn(null);

我认为我必须模拟该调用的原因是因为Service中的delete方法在同一服务中调用了getById()方法,而该服务又调用了EntityTypeRepository.findById(id).get()

就是这样,我假设我必须模拟删除操作。但显然我错了。任何帮助将不胜感激。

非常感谢

1 个答案:

答案 0 :(得分:0)

@Test
public void whenDelete_thenObjectShouldBeDeleted() {                
    final EntityType entity = new EntityType(1L, "new OET");
    Optional<EntityType> optionalEntityType = Optional.of(entity);
     Mockito.when(entityTypeRepository.findById(1L)).thenReturn(optionalEntityType);

    // when
    entityTypeService.delete(entity.getID());

    // then
    Mockito.verify(entityTypeRepository, times(1)).delete(entity);
    //I dont think you need to assert to confirm actual delete as you are testing mock registry. to assert somethink like below you need to return null by mocking the same call again and return the null but thats of no use
    //assertThat(entityTypeRepository.findById(1L).get()).isNull();       
 }

更新了您的测试。基本上,我们首先需要模拟findById的结果。请参阅上面我的评论,声明实际的删除。