cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to write unit test for controllers

Former Member
0 Likes
875

We are writing unit test for commerce webservices (OCC) controllers, for example CartsControllers, UsersController etc. Almost all the methods within these controllers return web service DTO i.e the ones that ends with *WsDTO. This object conversion is done by dataMapper which is part of spring web application context. The challenge we are facing is unit test or integration tests cannot access web application context and fetch the bean from there. 90% of commerce webservices (OCC) controller methods are not testable without this since they all return DTOs. Mocking dataMapper itself will not achieve anything since that will defeat the purpose of writing test.

Please help!!

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Likes

MockMvc - normal controllers

Rest Assured - Rest Controllers

As a side point anyone managed to configure spring security with MockMvc and a "standaloneSetup"

regards

Former Member
0 Likes

You could use http://rest-assured.io/ it's pretty cool.

Example

 @Test 
 public void
 lotto_resource_returns_200_with_expected_id_and_winners() {
     
     when().
             get("/lotto/{id}", 5).
     then().
             statusCode(200).
             body("lotto.lottoId", equalTo(5), 
                  "lotto.winners.winnerId", containsOnly(23, 54));
 
 }
Former Member
0 Likes

Hi Amit,

I think this article might contain some useful information: https://dzone.com/articles/testing-data-transfer-object-and-rest-controllers

Hope this helps,