简体   繁体   中英

Test a particular method with rest-assured in Java

I want to test a specific method in a class named AdRestService that has some @GET, @POST, etc. I want to test it from another testing class named AdRestServiceTest. I want to know how can I call a method in AdRestService from AdRestServiceTest and how can I test if the return is ok or not.

public class AdRestService {

    @GET
    @Path("{id}")
    @Produces("application/json")
    public Ad get(@PathParam("id") Long adId) {
        return adService.get(adId); // This points to the DB
    }

}

Now the rest-assured testing :

public class AdRestServiceTest {

    @InjectMocks
    private AdRestService adRestService;

    @Test
    public void getAdTest() {
        given().
        when().
            get("website.com/ad").
        then().
            assertThat().
            statusCode(200).
        and().
            contentType(ContentType.JSON).
        and().
            // ???
                // How can I call the method get(@PathParam("id") Long adId) from AdRestService and test if the return is correct ?
    }




}

I guess you are mixing up integration and unit tests . Rest-assured is used for integration tests, ergo they test whether your application server is responding properly to your defined requests. You would actually define a .json file with an expected answer and you would match that with the actual response to check whether the serialization of your application server is working properly.

See this as an example.

If you want to test the methods in your AdRestService , you would write a common unit test and there is no need to use rest-assured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM