简体   繁体   English

Mockito。 正在创建的内部对象中的方法调用计数

[英]Mockito. Method invocation count in the internal object being created

I have some class and method: 我有一些类和方法:

class Verificator {

   private Dao dao;
   private VerifyConsts verifyConsts;

   @Autowired
   public Verificator(Dao dao, VerifyConstants verifyConsts) {
        this.dao = dao;
        this.verifyConsts = verifyConsts;
   }

   public List<Pair<Products, Problem>> verify(final List<Product> links) {
     //do smth
     return new ProductVerifyLink().verifyLinks(links, verifyConstants.getForisInstanceZoneId(), dao.getDate());
   }

}

I have another class: 我还有另一堂课:

public class ProductVerifyLink {

    public List<Pair<Products, Problem>> verifyLinks(List<Product> links,
            final ZoneId zone,
            final Timestamp date
    ) throws Exception {
        //do smth
        List<Pair<Product, Problem>> res = verification(ImmutableList.of(links), zone);
        return res.isEmpty() ? null : res;
    }

    private List<Pair<Product, Problem>> verification(List<Product> links, ZoneId zone) {
      //do smth
    }
    //other methods
}

I created test: 我创建了测试:

    @Test
    public void calcTest() throws Exception {   
        final DAO dao = mock(DAO.class);
        final VerifyConstants verifyConsts = mock(VerifyConstants.class);
        final Verificator verifyLinks = Mockito.spy(new Verificator(dao, verifyConsts));

        final List<Product> links = ImmutableList.of(
                createUmrsLink(true, 2042229422, dateTime));
        final List<Long> linkIds = links.stream().map(Product::getProductId).collect(Collectors.toList());

        final List<Pair<ProductUmrs, VerifyProblem>> pairs = verifyLinks.verify(links);
    }

I want to check that method ProductVerifyLink.verification called once and with the arguments necessary to me. 我想检查一次方法ProductVerifyLink.verification并使用我所需的参数。 How can use Mockito and JUnit make it? 如何使用MockitoJUnit做到这一点?

The whole problem is that the object of this class ( ProductVerifyLink ) is created inside, and outside I can not influence it. 整个问题是此类的对象( ProductVerifyLink )是在内部创建的,而在我外部则无法影响它。

You can't verify private methods with mockito, hence you can do it with PowerMock 您无法使用Mockito验证私有方法,因此可以使用PowerMock进行验证

But I can suggest you to test return of your public method, so you do also test get through your flow including private methods. 但是我建议您测试公共方法的返回值,因此也要测试包括私有方法在内的流程。

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

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