Minnal test module provide abstractions for testing out projects built using minnal. While Minnal Generator generates the test cases for your resources, there are instances where you want to write additional tests to test customized APIs or test out your domain models. This section takes you through these abstractions using example functionalities.
Minnal enforces your domain models to extend from ActiveJpa models. AcitveJpa does runtime byte code isnstrumentation on your model classes and hence your models should be tested using activejpa-test. Minnal also uses AutoPojo in the abstraction layer for auto populating the domain models. This eliminates you from constructing json/xml data test yourself.
Note
Minnal test module supports only TestNG. Support for Junit is not there at this moment.
To test your resources, you should extend the abstract resource test class org.minnal.core.resource.BaseResourceTest
. The abstract class has helper methods to construct the request and parse the resposne for output comparisons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class DummyResourceTest extends BaseResourceTest {
@Test
public void testPostRequest() {
Response response = call(request("/dummy_resource/", HttpMethod.POST), "your content");
assertEquals(response.getStatus(), HttpResponseStatus.CREATED);
DummyModel expected = new DummyModel();
DummyModel actual = serializer.deserialize(response.getContent(), DummyModel.class);
assertEquals(actual, expected);
}
@Test
public void testGetRequest() {
Response response = call(request("/dummy_resource/", HttpMethod.GET));
assertEquals(response.getStatus(), HttpResponseStatus.OK);
int actualCount = serializer.deserialize(response.getContent(), DummyModel.class).size();
assertEquals(actualCount, 2);
}
}
|
Testing your JPA resources is bit tricky. You will have to ensure the a new transaction is opened before starting the test and rolled back at the end. Minnal provides an abstract test class for testing JPA resources org.minnal.core.resource.BaseJPAResourceTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class OrderResourceTest extends BaseJPAResourceTest {
@Test
public void listOrderTest() {
org.minnal.example.domain.Order order = createDomain(org.minnal.example.domain.Order.class);
order.persist();
Response response = call(request("/orders/", HttpMethod.GET));
assertEquals(response.getStatus(), HttpResponseStatus.OK);
assertEquals(serializer.deserializeCollection(
response.getContent(), java.util.List.class,
org.minnal.example.domain.Order.class).size(),
(int) org.minnal.example.domain.Order.count());
}
@Test
public void createOrderTest() {
org.minnal.example.domain.Order order = createDomain(org.minnal.example.domain.Order.class);
Response response = call(request("/orders/", HttpMethod.POST,
Serializer.DEFAULT_JSON_SERIALIZER
.serialize(order)));
assertEquals(response.getStatus(), HttpResponseStatus.CREATED);
}
}
|
To test your domain models, your test class should extend the class org.activejpa.entity.testng.BaseModelTest
The base class takes care of byte code enhancement requirement of ActiveJpa and abstracts them out of your tests.
1 2 3 4 5 6 7 8 9 | public class OrderTest extends BaseModelTest {
@Test
public void testCreateOrder() {
Order order = new Order();
order.setCustomerEmail("dummyemail@dummy.com");
order.persist();
Assert.assertEquals(Order.where("customer_email", "dummyemail@dummy.com").get(0), order);
}
}
|
Take a look at activejap-test for more information on testing your models and for additional exmaples.