使用Springboot 2.2的MockMvc提供406

时间:2019-07-18 23:00:01

标签: java spring-boot spring-boot-test

我正在尝试使用Springboot 2.2和MockMvc创建一个示例项目。

测试在端点返回String时起作用。但是,当将返回类型更改为Object(JSON)时,它将失败并显示406。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0              org.springframework.boot         弹簧启动启动器父母         2.2.0.BUILD-SNAPSHOT                    com.example.sb22     演示springboot2     0.0.1-快照     演示springboot2     Spring Boot的演示项目

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

控制器类

@Controller
public class TestController {
    @RequestMapping(value = "/employees")
    @ResponseBody
    public Employee getAllEmployees() {
        return new Employee();
    }
 }

 // Simple class
 class Employee {
 }

测试文件

@RunWith(SpringJUnit4ClassRunner.class)

公共类TestControllerTest {

@InjectMocks
private TestController controller;

@Test
public void canReachUrl() throws Exception {
    MockMvc mvc = MockMvcBuilders.standaloneSetup(controller).build();
    mvc
            .perform(
                    MockMvcRequestBuilders
                            .get("/employees")
                            .accept(MediaType.APPLICATION_JSON)
            )
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isOk());
}

这给出了406。我是否想念一些东西?

0 个答案:

没有答案