RestAssured-身体中的对象抛出错误

时间:2017-08-13 17:17:21

标签: java rest-assured

我在测试中传递的对象

@Data
public class UserRequest {
@JsonProperty("name")
private final String name;
@JsonProperty("surname")
private final String surname;
@JsonProperty("email")
private final String email;
@JsonProperty("iaAdmin")
private final boolean isAdmin;

}

比我测试

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerIT {

@LocalServerPort
private int port;

@Test
public void testIsCreatingNewUser() throws IOException{
    given()
            .when()
            .body(new UserRequest("asd","sad","asd",false))//.body(TestGenerator.getUserRequest())
            .port(port)
            .post("/user/" + TestGenerator.randomUUID)
            .then()
            .statusCode(HttpStatus.SC_CREATED);
}

我收到了错误:

java.util.ServiceConfigurationError: com.fasterxml.jackson.databind.Module: Provider com.fasterxml.jackson.module.kotlin.KotlinModule could not be instantiated

并在底部堆栈

Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.DefaultConstructorMarker
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 79 more

发生了什么事?! Kotlin ...我使用带有spring boot v2.0 m3的java

2 个答案:

答案 0 :(得分:0)

我自己回答问题。感谢@Sebastian Duque评论我添加了依赖项 http://www.baeldung.com/spring-boot-kotlin 它有帮助...

<!--kotlin/ it needs jackson to map objects-->
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jre8</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-reflect</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-kotlin</artifactId>
        <version>2.9.0</version>
    </dependency>


但奇怪的是Spring Boot v2中的Jackson需要Kotlin依赖项......我使用来自spring-boot starters的jackson依赖项,所以我没有包含任何杰克逊。

答案 1 :(得分:0)

如果您使用来自spring-boot依赖项的maven排除

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.0.0.M6</version>
    <exclusions>
        <exclusion>
                <groupId>com.fasterxml.jackson.module</groupId>
                <artifactId>jackson-module-kotlin</artifactId>
        </exclusion>
    </exclusions>
 </dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.0.0.M6</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
           <groupId>com.fasterxml.jackson.module</groupId>
           <artifactId>jackson-module-kotlin</artifactId>
        </exclusion>
    </exclusions>
</dependency>

对于其他构建工具,请更新此解决方案