private Link getBillingAgreementApprovalUrl(ScimRequest scimRequest, PaymentsManager pm,
TokenContext tokenContext, StringBuilder calMsg) throws ScimException {
calMsg.append("&settingUpBillingAgreements");
PaymentDetails pd = pm.createBillingAgreement(scimRequest);
calMsg.append("&baApprovalUrl=").append(pd.getBillingAgreementApprovalUrl());
tokenContext.setPaymentDetails(pd);
tokenManager.persistTokenContext(scimRequest);
Link link = new Link();
link.setHref(pd.getBillingAgreementApprovalUrl());
link.setRel("approval_url");
link.setMethod("REDIRECT");
return link;
}
3 Answers
The unit test should take in the arguments from the function and then make sure, that the Link object is constructed correctly via certain assert
statements. For this unit test you should either assume that pm.createBillingAgreement(scimRequest)
and persistTokenContext(scimRequest)
work correctly or - even better - already have unit tests for those two calls.
Your unit test for getBillingAgreementApprovalUrl()
must not test that those other two calls work.

- 977
- 3
- 12
-
could you please write unit tests for this – ARP Sep 08 '21 at 04:23
Firstly this is a private
method, so you probably shouldn't be testing it directly. Or perhaps your class is doing too much and should be split up.
Use a mocking library like mockito to provide the specific results from calling methods on ScimRequest, PaymentsManager and TokenContext instances that you need to create the conditions for each test case you want to run on your code.

- 10,307
- 4
- 35
- 40
Prior to writing tests for this method, you need to make this method public if you really want to write tests for this method.
There are many paths which can be tested here for this method.
You can Mock
below part of your code based on various scenarios and then assert the link
returned by the method.
calMsg.append("&settingUpBillingAgreements");
PaymentDetails pd = pm.createBillingAgreement(scimRequest);
calMsg.append("&baApprovalUrl=").append(pd.getBillingAgreementApprovalUrl());
tokenContext.setPaymentDetails(pd);
tokenManager.persistTokenContext(scimRequest);
For e.g. you can Mock PaymentDetails
class and then return the various values from createBillingAgreement
method.
// Declare PaymentDetails Mock
@Mock
PaymentDetails pd;
// Inside your each test cases
// Note below is just example, adjust as per your spec.
when(pd.createBillingAgreement(scimRequest)).thenReturn(new PaymentDetails("href://billing-approval-url"));
Refer to following links to understand mockito framework better :
https://www.tutorialspoint.com/mockito/mockito_create_mock.htm

- 2,092
- 3
- 17
- 36