简体   繁体   English

使用 Mockito 在 Spring Boot 测试中模拟 MapperFacade 返回

[英]Mocking MapperFacade return in Spring Boot Test with Mockito

I haven't seen this particular question answered in a way that's resolving my issue.我还没有看到以解决我的问题的方式回答这个特定问题。

I have a service class that is constructed with a repository and an Orika MapperFacade.我有一个使用存储库和 Orika MapperFacade 构建的服务类。 In testing, I'm arranging a list of Entities returned by the repository call.在测试中,我正在安排存储库调用返回的实体列表。 my doReturn(testList).when(testRepository).findAllBy... seems to be working.我的 doReturn(testList).when(testRepository).findAllBy... 似乎在工作。 My actual result list contains the same amount of elements (.size()) I've specified in my mocked list, however the values are all null.我的实际结果列表包含我在模拟列表中指定的相同数量的元素 (.size()),但是这些值都是空的。 I'm fairly new to Spring Boot, and definitely new to Mockito.我对 Spring Boot 相当陌生,对 Mockito 绝对是新手。 I'm currently using a bean that returns a MapperFacade created with a MapperFactory and has several class maps defined.我目前正在使用一个 bean,它返回一个用 MapperFactory 创建的 MapperFacade,并定义了几个类映射。 Right now I'm using a @Spy for my MapperFacade in test, and can pass based strictly on asserting that the size of the lists is the same.现在我在测试中为我的 MapperFacade 使用 @Spy,并且可以严格基于断言列表的大小相同来通过。 I understand it might be that I need to @Mock the behavior or the MapperFacade instead, although it's being used to iterate over a list in the service class.我理解可能是我需要 @Mock 行为或 MapperFacade,尽管它被用于迭代服务类中的列表。 Not positive how to implement that in test.不确定如何在测试中实现它。 I could build another list of DTO's that would generally be returned from a loop iterating through the entities, but I'm new to Mockito grammar and not sure how I'd run that return.我可以构建另一个 DTO 列表,这些列表通常会从遍历实体的循环中返回,但我是 Mockito 语法的新手,不确定如何运行该返回。

I get a NullPointerException when trying to log or assert on a .getAnyValue from an element of the list, but the size of the list changes when I add or remove elements in the arranged list.尝试从列表的元素中记录或断言 .getAnyValue 时,我收到 NullPointerException,但当我在排列的列表中添加或删除元素时,列表的大小会发生变化。

Any info appreciated.任何信息表示赞赏。

Service class服务类

public class FightService {
    private FightRepository repository;
    private MapperFacade mapper;

    @Autowired
    public FightService(FightRepository r, MapperFacade m) {
        this.repository = r;
        this.mapper = m;


    }

    public List<FightDTO> getFightsByWinRound(int winRound){

        List<FightEntity> entities = repository.findAllByWinRound(winRound);
        List<FightDTO> returnVal = new ArrayList<>();
        for(FightEntity e: entities){
            FightDTO dto = mapper.map(e, FightDTO.class);
            returnVal.add(dto);
        }
        return returnVal;
    }
}

Service Test服务测试

@Slf4j
@RunWith(MockitoJUnitRunner.class)
public class FightServiceTest{

    @Spy
    private MapperFacade mapperFacade;

    @Mock
    private FightRepository testRepository;

    @InjectMocks
    private FightService systemUnderTest;




    @Test
    public void testGetFightsByWinRound_ScenarioA() throws Exception{
        //Arrange

        List<FightEntity> testList = new ArrayList<>();
        FightEntity fightA = new FightEntity();
        fightA.setFighter1("A");
        fightA.setWinRound(15);
        testList.add(fightA);
        FightEntity fightB = new FightEntity();
        fightB.setFighter1("B");
        fightB.setWinRound(15);
        testList.add(fightB);
        FightEntity fightC = new FightEntity();
        fightC.setFighter1("C");
        fightC.setWinRound(15);
        testList.add(fightC);

        doReturn(testList).when(testRepository).findAllByWinRound(15);

        

        //Act

        List<FightDTO> actual = systemUnderTest.getFightsByWinRound(15);


        //Assert

        Assert.assertThat(actual.size(), is(3));
        // I would be adding Assert.assertThat(actual.get(0).getFighter1(), is("A")) , but this is where the NPE arises.
    


        //Verify
    }
}

This worked for这适用于

@InjectMocks
private ServiceClass serviceClass;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    factory = new DefaultMapperFactory.Builder().build();
    when(mapper.getMapperFacade()).thenReturn(factory.getMapperFacade());
    // In case of custom mapping
    factory.classMap(Source.class, Dest.class).customize(new CustomMapper())
            .byDefault().register();
}

You can check some internal tests at https://github.com/elaatifi/orika/blob/master/tests/src/main/java/ma/glasnost/orika/test/converter/NumericConvertersTestCase.java#L113您可以在https://github.com/elaatifi/orika/blob/master/tests/src/main/java/ma/glasnost/orika/test/converter/NumericConvertersTestCase.java#L113检查一些内部测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM