如何在 Spring Boot WebMvcTest 中设置上下文路径 [英] How to set the context path in Spring Boot WebMvcTest

查看:61
本文介绍了如何在 Spring Boot WebMvcTest 中设置上下文路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的 Spring Boot 应用程序测试我的 Rest 控制器,并希望控制器在与生产中相同的路径下可用.

I'm trying to test my Rest controllers from my Spring Boot application and want the controllers to be available under the same path as in production.

例如我有以下控制器:

@RestController
@Transactional
public class MyController {

    private final MyRepository repository;

    @Autowired
    public MyController(MyRepository repository) {
        this.repository = repository;
    }

    @RequestMapping(value = "/myentity/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<Resource<MyEntity>> getMyEntity(
            @PathVariable(value = "id") Long id) {
        MyEntity entity = repository.findOne(id);

        if (entity == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity<>(entity, HttpStatus.OK);
    }
}

在我的 application.yml 中,我已经为应用程序配置了上下文路径:

Within my application.yml I have configured the context path for the application:

server:
  contextPath: /testctx

我对这个控制器的测试如下:

My test for this controller looks as follows:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = MyController.class, secure=false)
public class MyControllerTest {

    @Autowired
    private MyRepository repositoryMock;

    @Autowired
    private MockMvc mvc;

    @Test
    public void testGet() throws Exception {
        MyEntity entity = new MyEntity();
        entity.setId(10L);
        when(repositoryMock.findOne(10L)).thenReturn(entity);

        MockHttpServletResponse response = this.mvc.perform(
            MockMvcRequestBuilders.get("/testctx/myentity/10"))
            .andReturn().getResponse();
        assertEquals(response.getStatus(), 200);
    }

    @TestConfiguration
    public static class TestConfig {
        @Bean
        MyRepository mockRepo() {
            return mock(MyRepository.class);
        }
    }
}

此测试失败,因为呼叫的状态代码为 404.如果我调用 /myentity/10 它可以工作.不幸的是,其余调用是由 CDC-Test-Framework (pact) 发起的,因此我无法更改请求的路径(包含上下文路径 /testctx).那么有没有办法告诉 Spring Boot 测试在测试期间也用定义的上下文路径启动其余端点?

This test fails since the status code is 404 for the call. If I call /myentity/10 it works. Unfortunately the rest call is initiated by a CDC-Test-Framework (pact) so I cannot change the requested path (containing the context path /testctx). So is there a way to tell spring boot test to start the rest endpoint with a defined context path also during testing?

推荐答案

你可以试试:

@WebMvcTest(controllers = {MyController.class})
@TestPropertySource(locations="classpath:application.properties")
class MyControllerTest {

    @Autowired
    protected MockMvc mockMvc;
    
    @Value("${server.servlet.context-path}")
    private String contextPath;
    
    @BeforeEach
    void setUp() {
        assertThat(contextPath).isNotBlank();
        ((MockServletContext) mockMvc.getDispatcherServlet().getServletContext()).setContextPath(contextPath);
    }
    
    protected MockHttpServletRequestBuilder createGetRequest(String request) {
        return get(contextPath + request).contextPath(contextPath)...
    }

这篇关于如何在 Spring Boot WebMvcTest 中设置上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆