Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing Unit testing for sdk functions #617

Closed
sanjayshrestha1 opened this issue Aug 25, 2021 · 5 comments
Closed

Writing Unit testing for sdk functions #617

sanjayshrestha1 opened this issue Aug 25, 2021 · 5 comments

Comments

@sanjayshrestha1
Copy link

I wanted to perform Unit Test the function which ultimately calls the sdk of okta so how can I mock the response for those sdk methods.
If there is any link that I could refer that would be great and any response is appreciated.

@sergiishamrai-okta
Copy link
Contributor

Hello @sanjayshrestha1 ,
Thanks for using OKTA SDK.

One of the samples with the mocked responses for example:
LogsTest

@sanjayshrestha1
Copy link
Author

Hi @sergiishamrai-okta, I am trying to run the okta-sdk-java in my machine but there is no class file named DefaultClient imported which is extended from MockClient class.

@sergiishamrai-okta
Copy link
Contributor

Hi @sanjayshrestha1,
Can you share your pom.xml dependencies?
I assume you didn't import all required dependencies.
The prerequisites for Java SDK usage is here

@sanjayshrestha1
Copy link
Author

The pom file contains the com.okta.sdk as defined in the prerequisites. I have also tried to replicate the code in java for unit test but instead of getting the mocked data from json file as mentioned in https://github.com/okta/okta-sdk-java/blob/master/impl/src/test/groovy/com/okta/sdk/impl/resource/log/LogsTest.groovy this link it is getting from the users from the specified client configured via baseurl and apikey. I just want to have the mock object from json file instead of from the api calling using client information.

@P1ll3P4ll3
Copy link

P1ll3P4ll3 commented Mar 3, 2022

It seems that the support here links to files contained in modules not available with the default prerequisite dependencies for the Okta Java SDK.

We have the same problem and question on how to get the Okta API Calls isolated, e.g. via Mockito, returning User or UserList objects. To be able to create those objects directly, a client is needed.

If we would use Mockito to isolate calls, then it must be possible to say something like:
User user = new User();
user.setLastname...
UserList userList = new UserList();
userList.set(user);
when( testee.client.getUserList(anyString()) ).thenReturn(userList);

Or something like that:
final UserList userListStub = Mockito.mock(UserList.class);
when(userListStub.single()).thenReturn(Mockito.mock(User.class));

final Client clientStub = Mockito.mock(Client.class);
when(clientStub.listUsers(anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(userListStub);

But it is still not possible to create objects to test logic of the own coding.

An additional API should be provided to get better support for unit testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants