Spring Boot Okta sdk user creation unit test

I am trying to create a unit test for a okta sdk user creation
but I am not sure where to start

I am passing user class to my metod and using sdk to create a user

Client client = Clients.builder()
.setOrgUrl("{yourOktaDomain}")
.setClientCredentials(new TokenClientCredentials("{apiToken}"))
.build();

User user = UserBuilder.instance()
.setEmail("joe.coder@example.com")
.setFirstName(“Joe”)
.setLastName(“Code”)
.buildAndCreate(client);

1 Like

Hey @hernan_plascencia!

What mocking framework are you using? I’d recommend injecting or otherwise reusing your Client object, that way you can mock the client and not worry about any HTTP traffic (for your unit test)

The next thing you need to think about is how you want to mock the UserBuilder.instance() class, you have a few options. It’s a static method so you could use something like Powermock. You could also mock out the interactions the UserBuilder has with the client but that gets into some details your implementation doesn’t (and shouldn’t) care about.

If you are using these static methods, I’d recommend exposing those static methods in your class so the implementation could be overwritten by your test.

Overly simplistic example:

class MyService {

  private final Client client;
  
  MyService(Client client) {
    this.client = client;
  }

  void doStuff() {
   User user = userBuilder()
     .setEmail("joe.coder@example.com")
     .setFirstName(“Joe”)
     .setLastName(“Code”)
     .buildAndCreate(client);
  }

  UserBuilder userBuilder() {
    return UserBuider.instance();
  }
}

Then you could just override the userBuilder() in your test to return your mock/stub.

Keep us posted!

1 Like