Spring Boot-正文的TestRestTemplate LocalDate null

时间:2018-12-10 22:07:37

标签: java spring-mvc spring-boot integration-testing resttemplate

当我从外部访问该服务时,可以很好地解析LocalDate类型的两个字段,并且该服务可以正常工作。

但是当我尝试使用TestRestTemplate测试非常相同的端点时,字段以null的形式到达控制器。 有什么问题吗?

更新: 这个问题更严重。当我尝试测试另一项服务时,我发现由TestRestTemplate完成的反序列化使得将LocalDate的列表构建为空列表。 我尝试使用自定义序列化器和反序列化器,但没有任何效果。 = /

控制器

@RestController
public class ReservationController {


@PostMapping("/reservation")
public String makeReservation(@RequestBody Reservation Reservation) {
    return this.reservationService.makeReservation(Reservation);
}
}

测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppRunner.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ReservationControllerTest {

@LocalServerPort
private int port;

private TestRestTemplate restTemplate = new TestRestTemplate();


@Test
public void attemp4(){
    LocalDate from = LocalDate.of(2019,1, 1);
    LocalDate to = LocalDate.of(2019,1, 21);

    Reservation Reservation = new Reservation(from, to);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<Reservation> entity = new HttpEntity<>(Reservation, headers);

    String uri = "http://localhost:"+port+"/reservation";
    ResponseEntity<String> response = restTemplate.postForEntity(uri, entity, String.class);
    Assert.assertNotNull(response);
}

}

预订/ API DTO

public class Reservation {

private LocalDate checkIn;
private LocalDate checkOut;

public Reservation(LocalDate checkin, LocalDate checkout) {
    this.checkIn = checkin;
    this.checkOut = checkout;
}


public LocalDate getCheckIn() {
    return checkIn;
}

public LocalDate getCheckOut() {
    return checkOut;
}

}

以防万一,pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0

<groupId>com.mysystem</groupId>
<artifactId>spring-boot-2-jdbc-with-h2</artifactId>
<version>0.0.4-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <findbugs-maven-plugin.version>3.0.4</findbugs-maven-plugin.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>27.0.1-jre</version>
    </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>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </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>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

<profiles>
    <profile>
        <id>integration-test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>add-test-source</id>
                            <phase>initialize</phase>
                            <goals>
                                <goal>add-test-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>src/integration-test/java</source>
                                    <source>src/test/java</source>
                                </sources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>add-test-resource</id>
                            <phase>initialize</phase>
                            <goals>
                                <goal>add-test-resource</goal>
                            </goals>
                            <configuration>
                                <resources>
                                    <resource>
                                        <directory>src/integration-test/resources</directory>
                                    </resource>
                                    <resource>
                                        <directory>src/test/resources</directory>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <runOrder>random</runOrder>
                    </configuration>
                </plugin>
            </plugins>
        </build>
</profiles>

谢谢!

0 个答案:

没有答案