Testing a Rapidoid rest api
10th of March, 2020In this post i will try to explain how to write an integration test for rest apis written using Rapidoid.
To test your rest api, there are two things need to do:
- extend your test class with RapidoidIntegrationTest
- Annotate your test class with @IntegrationTest, and pass the main class parameter.
Something like below:
@IntegrationTest(main = Main.class)
public class UserIntegrationTest extends RapidoidIntegrationTest {
}
For writing the test case, it is simple:
- Use Self class for performing all the rest calls.
like below:
@Test
public void shouldInsertUser() {
Map<String, ?> user1 = U.map("name", "Kuldeep");
User user = Self.post("/users").data(user1).toBean(User.class);
Assert.assertThat(user.getId(), Is.is(1L));
}
In addition to the assertion api provided by Junit, RapidoidTest provides some sugarcoated methods for assertion.