首页 文章

Spring MockMvc修剪参数

提问于
浏览
1

我遇到了一个让我难过的问题 . 我正在运行最新版本的Spring和JUnit . 我正在尝试使用MockMvc和Spring集成测试类,以确定它是否符合我公司的需求并且运行良好,直到我尝试提交包含@PathVariable的请求,该@PathVariable是url中的版本号 .

双响炮;

<modelVersion>4.0.0</modelVersion>

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>
  <groupId>asd</groupId>
  <artifactId>asd</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>asd</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>4.1.6.RELEASE</spring.version>
  </properties>

  <dependencies>
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>compile</scope>
        </dependency>
    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
            <version>4.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

  </dependencies>

示例控制器; import junit.framework.Assert;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleController {

    @RequestMapping(value = "/example/appVersion/{applicationVersion}")
    public @ResponseBody void example(@PathVariable("applicationVersion") String applicationVersion) {

        if (!"1.0.0.0".equals(applicationVersion)) {
            Assert.fail("I needed to be 1.0.0.0");
        }

    }
}

测试类;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:appContext.xml")
@WebIntegrationTest
public class ExampleProblem {

    @Autowired
    WebApplicationContext context;

    @Test
    public void test() throws Exception {
        String url = "/example/appVersion/1.0.0.0";
        // String url = "/example/appVersion/{applicationVersion}";

        //@formatter:off
        MvcResult result =  webAppContextSetup(context)
        .build()
        .perform(
            get(url)
//                              get(url, "1.0.0.0")
            .accept("application/json")
        ).andDo(print())
        .andExpect(status().is(200))
        .andReturn();
        //@formatter:on

        Assert.assertNotNull(result);
    }
}

我的appContext.xml文件只包含这一行(初始bean标记和结束标记除外);

<context:component-scan base-package="asd" />

当我查看进入我的示例控制器的请求时,我可以使用调试器看到/example/appVersion/1.0.0.0,但路径变量始终设置为“1.0.0” . 看起来最后的.0总是被丢弃,因为如果我传入“1.0.0.0.0”它会修剪到预期的“1.0.0.0” .

任何洞察可能导致这个或我做错的事情都将非常感激 .

谢谢 .

1 回答

  • 0

    这是因为Spring将最后一个点( . )和后面的字符解释为文件扩展名 .

    为了解决这个问题,你需要通过将 ":.+" 添加到controller参数的末尾来告诉Spring该参数需要点字符 . 所以注释现在看起来像这样;

    @RequestMapping(value = "/example/appVersion/{applicationVersion:.+}")
    

相关问题