jeudi 12 décembre 2019

Test Junit of jhipster on a post service?

I'm using Jhipster and I generate a controller using "jhipster generate-controller NAME-OF-MY-CONTROLLER" this create the hulled of a service and a test, in a post service I add a parameter of type LocalDate, initially I had errors about data type, because I was used a string, but finally I used .param("date", "02.11.2019") and the test don't show more error about data type, altrought I don't know if this way it's fine, now this test gives me the next error (NestedServlet Request Process):

enter image description here

My actual code of the test is:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.fasterxml.jackson.databind.ObjectMapper;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Test class for the NotificationResource REST controller.
 *
 * @see NotificationResource
 */
@SpringBootTest(classes = MiApp.class)
public class NotificationResourceIT {

    private MockMvc restMockMvc;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        NotificationResource notificationResource = new NotificationResource();
        restMockMvc = MockMvcBuilders
            .standaloneSetup(notificationResource)
            .build();
    }

    public static String asJsonString(final Object obj) {
        try {
            return new ObjectMapper().writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Test postNotification
     */
    @Test
    public void testPostNotification() throws Exception {
        ResultMatcher ok = MockMvcResultMatchers.status().isOk();
        restMockMvc.perform(post("/api/notification/post-notification")
        .param("date", "02.11.2019")
        )
        .andExpect(ok);
    }
}

And the post service is this:


import java.time.LocalDate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;


/**
 * NotificationResource controller
 */
@RestController
@RequestMapping("/api/notification")
public class NotificationResource {

    @Autowired
    private CmsService cmsService;

    /**
    * POST postNotification
     * @throws MeshRestClientPoolException 
    */

    @RequestMapping(value = "/post-notification", method = RequestMethod.POST,produces = "application/json")
    public String postNotification(@RequestParam("date") 
                            @DateTimeFormat(pattern = "dd.MM.yyyy") LocalDate date) throws MeshRestClientPoolException {
        String json = new Gson().toJson(cmsService.getSentNotifications(date));
        return json;
    }

}

I will apreciate much your help

Note: I ommited some imports in the code, for hide the company name, but the imports it's fine

Aucun commentaire:

Enregistrer un commentaire