[英]Mockito mocking a function call in an if statement
I am trying to mock a function call to an external api that lives in an if
statement. 我正在尝试模拟对驻留在if
语句中的外部api的函数调用。 I am not able to return the value I have in .thenReturn
, and I am not sure why. 我无法返回.thenReturn
的值,并且不确定为什么。 I have looked for answers to this on SO, but I can't seem to find anything that answers my question. 我一直在寻找有关此问题的答案,但似乎找不到任何能回答我问题的答案。 Thanks a bunch for your time! 感谢您的宝贵时间!
Here is my class I am testing: 这是我正在测试的班级:
@Service
public class TwilioVerifyService {
public String requestCode(String phoneNumber, String countryCode, String via) throws AuthyException
{
AuthyApiClient authyApiClient = new AuthyApiClient("<apiClient>");
Params params = new Params();
params.setAttribute("code_length", "6");
Verification verification = authyApiClient
.getPhoneVerification()
.start(phoneNumber, countryCode, via, params);
if (verification.isOk())
{
return "{ \"success\": \"Successfully sent verification code.\" }";
}
return "{ \"error\": \"Error sending code.\" }";
}
}
And here is my test: 这是我的测试:
@RunWith(MockitoJUnitRunner.class)
public class TwilioVerifyServiceTests {
@InjectMocks
TwilioVerifyService twilioVerifyService;
@Mock
Verification verification;
@Test
public void requestCodeTest_success() throws AuthyException
{
String phoneNumber = "1111111111";
String countryCode = "1";
String via = "1";
when(verification.isOk()).thenReturn(true);
String result = twilioVerifyService.requestCode(phoneNumber, countryCode, via);
System.out.println(result);
}
}
I believe I'm (or want to be) mocking out verification.isOk()
to return true regardless of the inputs, but it seems to return false providing "{ "error": "Error sending code." }"
instead of "{ \\"success\\": \\"Successfully sent verification code.\\" }"
. 我相信我正在(或希望成为)模拟出verification.isOk()
不论输入如何都返回true,但是如果提供"{ "error": "Error sending code." }"
而不是"{ \\"success\\": \\"Successfully sent verification code.\\" }"
,它似乎返回false "{ "error": "Error sending code." }"
"{ \\"success\\": \\"Successfully sent verification code.\\" }"
。
Thanks again for your time! 感谢你的宝贵时间!
Verification is generated from a call to methods in AuthyApiClient. 验证是通过对AuthyApiClient中的方法的调用生成的。
Ideally AuthyApiClient should not be instantiated in your service, but rather injected into it by the caller. 理想情况下,不应在您的服务中实例化AuthyApiClient,而应由调用者将其注入其中。
private AuthyApiClient authyApiClient;
@Autowired
public TwilioVerifyService(AuthyApiClient authyApiClient) {
this.authyApiClient = authyApiClient;
}
Then you can mock authyApiClient and pass it in to the class being tested: TwilioVerifyService twilioVerifyService = new TwilioVerifyService(mockAuthyApiClient); 然后,您可以模拟authyApiClient并将其传递给要测试的类:TwilioVerifyService twilioVerifyService = new TwilioVerifyService(mockAuthyApiClient);
That gives you more control over the class being tested and removes the dependency it currently has on the AuthyApiClient constructor. 这使您可以更好地控制要测试的类,并删除其当前对AuthyApiClient构造函数的依赖关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.