我遇到了一个让我难过的问题。我正在运行最新版本的 Spring 和 JUnit。我正在试验 MockMvc 和 Spring 集成测试类,以查看它是否符合我公司的需求并且运行良好,直到我尝试提交包含 @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>
示例控制器;导入 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 文件只包含这一行(除了最初的 beans 标记和结束标记);
<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”。
任何对可能导致这种情况或我做错了什么的见解将不胜感激。
谢谢你。