mercredi 24 avril 2019

Unirest POST Testing

I want to test a method that makes a POST request with Unirest. So far, I've tried this. Unfortunately it didn't work. I would like to test the if Json object is sent correctly.

My Unirest Post class

try {
            HttpResponse<JsonNode> response = Unirest.post( "http://localhost:8080/url" )
                    .header( "Content-Type", "application/json" )
                    .body( jsonObject )
                    .asJson();
        }
        catch( Exception e ) {
            e.printStackTrace();
        }

Test class

@RunWith( MockitoJUnitRunner.class )
public class MyTest {


    @Mock
    private Post post;

    private JSONObject jsonObject;


    @Test
    public void Test() {
        jsonObject.put( "bla", "bla");
        verifyPostRequest();
        post.sendPost( jsonObject );

    }


    private void verifyPostRequest() {
        new MockServerClient("localhost", 8080).verify(
                request()
                        .withMethod("POST")
                        .withPath("/url")
                        .withBody(exact("{bla: 'bla'}")),
                VerificationTimes.exactly(1)
        );
    }
}

Aucun commentaire:

Enregistrer un commentaire