1

I have a controller to handle all my geometry's operations, it works just fine and it's already in use but, now that I try to make the tests it just doesn't work. I'm new at this test thing, but I think the problem is with JUnit that doesn't understand Geometry types.

The Controller's endpoint:

/**
 * Method that returns the geometry of a single entity.
 *
 * @param table Target table..
 * @param id    Entity's ID.
 * @return {@link Geometry}
 */
@GetMapping(value = "/get-geom/{id}")
public ResponseEntity<Geometry> getGeomByEntityId(@RequestHeader(value = "table") String table,
                                                    @RequestHeader(value = "projection",
                                                            required = false) Integer projection,
                                                    @RequestHeader(value = "schema",
                                                            required = false) String schema,
                                                    @PathVariable Long id, HttpServletRequest request) {
    String tenant = jwtUtils.getTenantFromToken(request.getHeader("Authorization"));
    return geometryService.getGeomByEntityId(schema, table, id, projection, tenant)
            .map(geom -> new ResponseEntity<>(geom, HttpStatus.OK))
            .orElseGet(() -> new ResponseEntity<>(null, HttpStatus.NO_CONTENT));
}

The test method:

    @Test
    void getGeomByEntityId() throws Exception {
    mockMvc = MockMvcBuilders.standaloneSetup(geometriaController).build();

    Mockito.when(jwtUtils.getTenantFromToken(Mockito.anyString())).thenReturn("palotina");
    Mockito.when(geometriaService
            .retornaGeomEntidade(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(),
            Mockito.anyInt(), Mockito.anyString())).thenReturn(Optional.of(geometria));

    mockMvc.perform(MockMvcRequestBuilders
            .get("/manage-geometries/get-geom/1")
            .header("table", "district")
            .header("projection", 4326)
            .header("schema", "unique_base")
            .header("Authorization", token))
            .andDo(print())
            .andExpect(status().isOk());
}

The stack Trace:

*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message transform method call failed at JPLISAgent.c line: 844
10:47:04.145 [main] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]
java.lang.IllegalStateException: Cannot set error status - response is already committed
    at org.springframework.util.Assert.state(Assert.java:76)
    at org.springframework.mock.web.MockHttpServletResponse.sendError(MockHttpServletResponse.java:543)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.sendServerError(DefaultHandlerExceptionResolver.java:550)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:440)
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:210)
    at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:141)
    at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80)
    at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1320)
    at org.springframework.test.web.servlet.TestDispatcherServlet.processHandlerException(TestDispatcherServlet.java:144)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1131)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1077)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:183)
    at br.com.maptriz.basecartografica.controller.GeometriaControllerTest.retornaGeomEntidade(GeometriaControllerTest.java:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:210)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
10:47:04.147 [main] DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - Failed to complete request: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]->com.vividsolutions.jts.geom.Polygon["envelope"]...

There's a lot more text but to summarise it, it's a stack overflow exception. I already know this error from past issues, it happens when you don't configure your "JTSModule", but I do have it configurated and it works fine, hitting with Postman with the same parameters. Do I need a "JTSModuleTest" dependency?

The entire test class:

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.geojson.GeoJsonReader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Optional;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
class GeometryControllerTest {

    GeometryController geometryController;

    @MockBean
    GeometryService geometryService;

    @MockBean
    HttpServletRequest request;

    @MockBean
    JWTUtils jwtUtils;

    @Autowired
    MockMvc mockMvc;

    String token;
    String geoJson;
    Geometry myGeometry;

    GeometryControllerTest() throws ParseException {
        token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJnYWJyaWVsLXRlbmFudDpwYWxvdGluYSIsImlhdCI6MTYxMTMzM"
                + "zM1MCwiZXhwIjoxNjExMzY5MzUwfQ.QH1a-sgtg3jfLb_WPfQ3l8r84vx-3gu6zYSyjHKOffnclfW1gqOVqK-Bd"
                + "iB8Sa2uFYufVR6P8vl1RT8lOFptvg";
        geoJson = "{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:31982\"}},"
                + "\"coordinates\":[[[212788.861971751,7308432.19],[212675.566900377,7308266.31],"
                + "[212610.908402093,7308310.47],[212679.715643766,7308411.21],[212665.677400206,7308420.8],"
                + "[212710.165229905,7308485.94],[212788.861971751,7308432.19]]]}";
        myGeometry = new GeoJsonReader().read("{\"type\":\"Polygon\",\"crs\":{\"type\":\"name\"," +
                "\"properties\":{\"name\":\"EPSG:31982\"}},\"coordinates\":[[[211671.728237639,7308594.83]," +
                "[211526.8924293,7308583.99],[211525.333636196,7308604.93],[211669.967798158,7308615.7]," +
                "[211671.728237639,7308594.83]]]}");
    }

    @BeforeEach
    void setup() {
        request = Mockito.mock(HttpServletRequest.class);
        geometryService = Mockito.mock(GeometryService.class);
        jwtUtils = Mockito.mock(JWTUtils.class);
        geometryController = new GeometryController(geometryService, jwtUtils);
    }

    @Test
    @Disabled("Stack overflow in this tests. SEND HELP!")
    void updateGeom() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(geometryController)
                .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
                .setViewResolvers((viewName, locale) -> new MappingJackson2JsonView()).build();

        Mockito.when(geometryService.updateGeom(null, "district", myGeometry, 1L, "myUser"))
                .thenReturn(true);

        mockMvc.perform(MockMvcRequestBuilders
                .post("/manage-geometries/update-geom/1").contentType(MediaType.APPLICATION_JSON)
                .content(geoJson)
                .header("table", "district")
                .header("Authorization", token))
                .andDo(print())
                .andExpect(status().isOk());
    }

    @Test
    @Disabled("Stack overflow in this tests. SEND HELP!")
    void getGeomByEntityId() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(geometryController).build();

        Mockito.when(jwtUtils.getTenantFromToken(Mockito.anyString())).thenReturn("myUser");
        Mockito.when(geometryService
                .getGeomByEntityId(Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(),
                        Mockito.anyInt(), Mockito.anyString())).thenReturn(Optional.of(myGeometry));

        mockMvc.perform(MockMvcRequestBuilders
                .get("/manage-geometries/pega-geom/1")
                .header("table", "quadra")
                .header("projection", 4326)
                .header("schema", "base_cartografica")
                .header("Authorization", token))
                .andDo(print())
                .andExpect(status().isOk());
    }

    @Test
    void getGeomByTable() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(geometryController).build();

        Mockito.when(geometryService.getGeomByTable(null, "quadra", null, "myUser"))
                .thenReturn(Arrays.asList(myGeometry, new GeoJsonReader().read(geoJson)));

        mockMvc.perform(MockMvcRequestBuilders
                .get("/manage-geometries/pega-geom/")
                .header("table", "quadra")
                .header("Authorization", token))
                .andDo(print())
                .andExpect(status().isOk());
    }

    @Test
    void getGeomByTableIdIn() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(geometryController).build();

        Mockito.when(geometryService.getGeomByTableIdIn(null, "lote", Arrays.asList(2l, 3l), "myUser"))
                .thenReturn(Arrays.asList(myGeometry, new GeoJsonReader().read(geoJson)));

        mockMvc.perform(MockMvcRequestBuilders
                .get("/manage-geometries/pega-geom-in")
                .header("ids", Arrays.asList(2l, 3l))
                .header("table", "quadra")
                .header("tenant", "myUser")
                .header("Authorization", token))
                .andExpect(status().isOk());
    }
}
Alfredo Marin
  • 195
  • 1
  • 1
  • 14
  • how do you configure your `JTSModule`? Are you using `@WebMvcTest` for your test class? Can you add your complete test class to understand the setup better? – rieckpil Feb 23 '21 at 14:34
  • Actually I'm using `@AutoConfigureMockMvc` on the class. I'll edit to put the entire class. And my configuration of the JTSModule was the simplest one. Just got a class called `JacksonConf` and inside I have only one method called `jtsModule()` which returns a `new JtsModule()`. This method is annotated with "@Bean". Just doing that was enough to make my endpoints work on postman so I didn't go any further than that. – Alfredo Marin Feb 23 '21 at 15:57
  • I assume the `JacksonConf` is annotated with `@Configuration`? This won't be part of your existing test context and you have to either `@Import` the config manually or provide it with a `@TestConfig`. Also note that [testing with `MockMvc`](https://rieckpil.de/guide-to-testing-spring-boot-applications-with-mockmvc/) can't be compared to running the application and using Postman to access it. – rieckpil Feb 23 '21 at 17:16
  • Yes! It is annotated with `@Configuration`. "Also note that testing with MockMvc can't be compared to running the application and using Postman to access it", I'll read it and got it! So I need to have a "@MockBean" inside tha test class of my "JacsonConf" class? – Alfredo Marin Feb 23 '21 at 17:22
  • 1
    You need e.g. `@Import(JacksonConf.class)` on top of your test class. – rieckpil Feb 23 '21 at 19:10
  • Didn't work! The error has changed a little ``Caused by: org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.vividsolutions.jts.geom.Geometry]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.vividsolutions.jts.geom.Geometry` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information at [Source: (PushbackInputStream); line: 1, column: 1]`` – Alfredo Marin Feb 24 '21 at 11:48

0 Answers0