vendredi 11 septembre 2020

How to mock this method in this case?

I would like to write a unit test for this method:

public class ClassToTest {
public boolean initIndex(String url) {
    try (CloseableHttpClient client = HttpClients.createDefault()) {
        URIBuilder uriBuilder = new URIBuilder(url);
        
        HttpGet httpGet = new HttpGet(uriBuilder.build());;
        
        try (CloseableHttpResponse response = client.execute(httpGet)) {
            HttpEntity responseEntity = response.getEntity();
            int status = response.getStatusLine().getStatusCode();
            
            EntityUtils.consume(responseEntity);
            
            return status == HttpStatus.SC_OK;
        }
        
    } catch (IOException | URISyntaxException ex) {
        
    }
}
}

My unit test fails here try (CloseableHttpResponse response = client.execute(httpGet))

I am not let to rewrite this method/class.

My test now:

public class UnitTestClass {

@InjectMocks
private ClassToTest classToTest;

@Test
public void testInitIndex() throws Exception {
        classToTest.initIndex();
}
}

Any idea?

Aucun commentaire:

Enregistrer un commentaire