Hello,
I have some custom dispatcher service serviceimplementation to be able to read request and response too:
@Bean
@Primary
@SuppressWarnings({"squid:MaximumInheritanceDepth"})
public DispatcherServlet dispatcherServlet(WebApplicationContext context) {
return new DispatcherServlet(context) {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
IOException {
MultiReadHttpServletRequest requestWrapper = new MultiReadHttpServletRequest(request);
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
super.service(requestWrapper, responseWrapper);
responseWrapper.copyBodyToResponse();
}
};
}
I also have custom HandlerInterceptor which logs content of request and response in preHandle and afterCompletion. Implementation is very basic so the details are useless here.
I have faced some curious issue. When I run application, the HandlerInterceptor's preHandle method is rightly executed after my custom service method and everyone is happy. If I run any @SpringBootTest then preHandle is called before my custom DispatcherServlet's service method implementation.
I have debugged it a little bit and my custom DispatcherSevlet is registered fine but it is never invoked. I hoped I would solve the issue by setting webEnvironment = RANDOM_PORT on @SpringBootTest annotation but it did not help either.
Here is my test setting
@SpringBootTest(
classes = {MainApplication.class, DatasourcesH2TestConfiguration.class},
properties = {
"spring.main.allow-bean-definition-overriding=true",
"spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect",
"spring.jpa.properties.hibernate.generate_statistics=false"
}
)
@WithMockUser
@AutoConfigureMockMvc
@ActiveProfiles("dev")
@RunWith(SpringRunner.class)
@Sql(scripts = {"classpath:sql/import-h2.sql"})
public abstract class AbstractH2Test {
}
What do you think? Thanks for responses.
Comment From: wilkinsona
Thanks for the report. Unfortunately, it's hard to tell what might be happening from what you've shared thus far. If you would like us to spend some more time investigating, please spend some time creating a minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: Drezir
Thanks for the report. Unfortunately, it's hard to tell what might be happening from what you've shared thus far. If you would like us to spend some more time investigating, please spend some time creating a minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Hello,
thanks for the response. I have just uploaded minimal example. Just run PingTest. Thanks test.zip
Comment From: wilkinsona
Thanks for the sample.
MockMvc uses its own DispatcherServlet extension, TestDispatcherServlet, so yours isn't used. If you want to test a custom DispatcherServlet of your own, you need to make a full-blown HTTP request using WebClient or TestRestTemplate. Here's your existing test updated to use the latter:
package cz.multima.hiskom.integration;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import cz.multima.hiskom.testconfiguration.AbstractH2Test;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class PingTest extends AbstractH2Test {
@Autowired
private TestRestTemplate rest;
@Test
public void test() throws Exception {
ResponseEntity<String> response = rest.getForEntity("/ping", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo("pong");
}
}