dest
dest

Reputation: 79

Test method with okhttp3 call

I have a method in the service class that makes a call to an external api. How would I mock this okHttpClient call? I have tried to do so with mockito but no luck with that.

//this is the format of the method that i want to test
public string sendMess(EventObj event) {
    OkHttpClient client = new OkHttpClient();
    //build payload using the information stored in the payload object
    ResponseBody body = 
        RequestBody.create(MediaType.parse("application/json"), payload);
    Request request = //built using the Requestbody
    //trying to mock a response from execute
    Response response = client.newCall(request).execute();
    //other logic
}

I am open to refactoring the service class if it helps with the testing. Any suggestions and recommendation is appreciated. Thanks.

Upvotes: 4

Views: 9506

Answers (2)

Ryuzaki L
Ryuzaki L

Reputation: 39978

Since you are using spring-boot leave managing beans to spring.

1) First create OkHttpClient as spring bean so that you can use it all over application

@Configuration
public class Config {

@Bean
public OkHttpClient okHttpClient() {
    return new OkHttpClient();
    }
 }

2) And then in the service class @Autowire OkHttpClient and use it

@Service
public class SendMsgService {

@Autowired
private OkHttpClient okHttpClient;

 public string sendMess(EventObj event) {

ResponseBody body =  RequestBody.create(MediaType.parse("application/json"), payload);
Request request = //built using the Requestbody
//trying to mock a response from execute
Response response = okHttpClient.newCall(request).execute();
//other logic
   }
 }

Tests

3) Now in the Test classes use @SpringBootTest,@RunWith(SpringRunner.class) and @MockBean

The @SpringBootTest annotation can be used when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests.

@RunWith(SpringRunner.class) is used to provide a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in out JUnit tests, this annotation will be required.

@MockBean Annotation that can be used to add mocks to a Spring ApplicationContext.

@SpringBootTest
@RunWith(SpringRunner.class)
public class ServiceTest {

 @Autowire
 private SendMsgService sendMsgService;

 @MockBean
 private OkHttpClient okHttpClient;

  @Test
  public void testSendMsg(){

 given(this.okHttpClient.newCall(ArgumentMatchers.any())
            .execute()).willReturn(String);

  EventObj event = //event object
 String result = sendMsgService.sendMess(event);

  }
 }

Upvotes: 5

mle
mle

Reputation: 2542

I would suggest, that you pull out the instantiation of your OkHttpClient to an own method in a Configuration class. Afterwards you could @Inject the client anywhere it is needed and testing becomes much easier because you can @Mock it away.

Such to say the Spring-managed bean:

@Configuration
public class OkHttpClientConfiguration {
    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient();
    }
}

…your production class:

@Component
public class ProductionClass {
    @Inject
    private OkHttpClient okHttpClient;

    public string sendMess(EventObj event) {
       okHttpClient // whatever you want
       […]
    }
}

…and your test:

public class SpyTest {
    @InjectMocks
    private ProductionClass productionClass;
    @Mock
    private OkHttpClient okHttpClient;


    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void spyInsteadOfPowermock() {
        Request request = // mock the request
        when(okHttpClient.newCall(request)).thenReturn(mock(Call.class));
    }
}

Upvotes: 0

Related Questions