简体   繁体   English

如何使用 junit 从另一个方法中测试方法调用

[英]How to test method call from within another method using junit

I am new to unit testing in java so please excuse if this is a silly question.我是 java 单元测试的新手,所以如果这是一个愚蠢的问题,请原谅。 I have a method like the one below.我有一种类似下面的方法。 What I want to do is verify that this method is being called and also check that the parameters that are passed are not null and also being called.我要做的是验证是否正在调用此方法,并检查传递的参数是否不是 null 并且也被调用。 How can I go about doing this in junit/mockito?我怎么能在 junit/mockito 中做到这一点?

public <return type> callMe(Object objectA) {

if(objectA.name != null && objectA.age != null) {
someOtherMethod(objectA.name, objectA.age)

  }
}

I think one alternative is using @Spy annotation to execute the method inside your first method and after that check the result.我认为另一种方法是使用@Spy注释在您的第一个方法中执行该方法,然后检查结果。 https://www.baeldung.com/mockito-spy https://www.baeldung.com/mockito-spy

You can also use the Mockito.veify(...) to check if the method was called.您还可以使用Mockito.veify(...)检查该方法是否被调用。 https://www.baeldung.com/mockito-verify https://www.baeldung.com/mockito-verify

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

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