91

我一直在为一个简单的 Spring Web 应用程序编写一些简单的单元测试例程。当我在资源的 getter 方法上添加 @JsonIgnore 注释时,生成的 json 对象不包含相应的 json 元素。因此,当我的单元测试例程尝试测试这是否为空(这是我的情况的预期行为,我不希望密码在 json 对象中可用)时,测试例程会遇到异常:

java.lang.AssertionError:JSON 路径没有值:$.password,异常:路径没有结果:$['password']

这是我写的单元测试方法,用 is(nullValue()) 方法测试“密码”字段:

@Test
public void getUserThatExists() throws Exception {
    User user = new User();
    user.setId(1L);
    user.setUsername("zobayer");
    user.setPassword("123456");

    when(userService.getUserById(1L)).thenReturn(user);

    mockMvc.perform(get("/users/1"))
            .andExpect(jsonPath("$.username", is(user.getUsername())))
            .andExpect(jsonPath("$.password", is(nullValue())))
            .andExpect(jsonPath("$.links[*].href", hasItem(endsWith("/users/1"))))
            .andExpect(status().isOk())
            .andDo(print());
}

我也尝试过使用 jsonPath().exists() 得到类似的异常,指出路径不存在。我正在分享更多代码片段,以便整个情况变得更具可读性。

我正在测试的控制器方法如下所示:

@RequestMapping(value="/users/{userId}", method= RequestMethod.GET)
public ResponseEntity<UserResource> getUser(@PathVariable Long userId) {
    logger.info("Request arrived for getUser() with params {}", userId);
    User user = userService.getUserById(userId);
    if(user != null) {
        UserResource userResource = new UserResourceAsm().toResource(user);
        return new ResponseEntity<>(userResource, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

我正在使用 spring hatos 资源组装器将实体转换为资源对象,这是我的资源类:

public class UserResource extends ResourceSupport {
    private Long userId;
    private String username;
    private String password;

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @JsonIgnore
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

我理解为什么这会出现异常,在某种程度上,测试是成功的,它找不到密码字段。但我想做的是,运行这个测试以确保该字段不存在,或者如果存在,它包含空值。我怎样才能做到这一点?

堆栈溢出中有一个类似的帖子: Hamcrest with MockMvc: check that key exists but value may be null

就我而言,该字段也可能不存在。

作为记录,这些是我正在使用的测试包的版本:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path-assert</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency>

提前致谢。

[编辑] 更准确地说,你必须为一个实体编写一个测试,你知道其中一些字段需要为空或空或者甚至不应该存在,并且你实际上并没有通过代码来查看如果在属性顶部添加了 JsonIgnore。你希望你的测试通过,我该怎么做。

请随时告诉我,这根本不实用,但仍然很高兴知道。

[编辑] 上述测试通过以下较旧的 json-path 依赖项成功:

    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>0.9.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path-assert</artifactId>
        <version>0.9.1</version>
        <scope>test</scope>
    </dependency>

[编辑] 在阅读了 spring 的 json 路径匹配器的文档后,找到了一个适用于最新版本 jayway.jasonpath 的快速修复。

.andExpect(jsonPath("$.password").doesNotExist())
4

5 回答 5

188

我对新版本有同样的问题。在我看来,doesNotExist() 函数将验证密钥不在结果中:

.andExpect(jsonPath("$.password").doesNotExist())
于 2016-01-22T16:39:51.457 回答
3

@JsonIgnore 的行为符合预期,没有在 json 输出中生成密码,所以你怎么能期望测试你明确排除在输出之外的东西呢?

该行:

.andExpect(jsonPath("$.property", is("some value")));

甚至是属性为空的测试:

.andExpect(jsonPath("$.property").value(IsNull.nullValue()));

对应一个json,如:

{
...
"property": "some value",
...
}

其中重要的部分是左侧,即“属性”的存在:

相反,@JsonIgnore 根本不会在输出中生成该属性,因此您不能指望它不在测试或生产输出中。如果您不想在输出中使用该属性,那很好,但您不能期望它在测试中出现。如果您希望它在输出中为空(在 prod 和 test 中),您希望在中间创建一个静态 Mapper 方法,该方法不将属性的值传递给 json 对象:

Mapper.mapPersonToRest(User user) {//exclude the password}

然后你的方法是:

@RequestMapping(value="/users/{userId}", method= RequestMethod.GET)
public ResponseEntity<UserResource> getUser(@PathVariable Long userId) {
    logger.info("Request arrived for getUser() with params {}", userId);
    User user = Mapper.mapPersonToRest(userService.getUserById(userId));
    if(user != null) {
        UserResource userResource = new UserResourceAsm().toResource(user);
        return new ResponseEntity<>(userResource, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

此时,如果您期望 Mapper.mapPersonToRest 返回一个密码为空的用户,您可以在此方法上编写一个正常的单元测试。

PS当然密码是在数据库上加密的,对吧?;)

于 2015-09-04T12:38:28.140 回答
2

存在但null有价值的财产与根本不存在的财产之间存在差异。

如果仅当存在非值时测试才失败null,请使用:

.andExpect(jsonPath("password").doesNotExist())

如果属性一出现测试就失败了,即使有一个null值,使用:

.andExpect(jsonPath("password").doesNotHaveJsonPath())
于 2020-10-24T07:51:42.717 回答
0

我想重用我用来测试提供的参数的相同代码,因为它丢失了,这就是我想出的

  @Test
  void testEditionFoundInRequest() throws JsonProcessingException {
    testEditionWithValue("myEdition");
  }

  @Test
  void testEditionNotFoundInRequest() {
    try {
      testEditionWithValue(null);
      throw new RuntimeException("Shouldn't pass");
    } catch (AssertionError | JsonProcessingException e) {
      var msg = e.getMessage();
      assertTrue(msg.contains("No value at JSON path"));
    }
  }


  void testEditionWithValue(String edition) {   
   var HOST ="fakeHost";
   var restTemplate = new RestTemplate();
   var myRestClientUsingRestTemplate = new MyRestClientUsingRestTemplate(HOST, restTemplate);
   MockRestServiceServer mockServer;
   ObjectMapper objectMapper = new ObjectMapper();
   String id = "userId";
   var mockResponse = "{}";

   var request = new MyRequest.Builder(id).edition(null).build();
   mockServer = MockRestServiceServer.bindTo(restTemplate).bufferContent().build();

   mockServer
        .expect(method(POST))

        // THIS IS THE LINE I'd like to say "NOT" found
        .andExpect(jsonPath("$.edition").value(edition))
        .andRespond(withSuccess(mockResponse, APPLICATION_JSON));

    var response = myRestClientUsingRestTemplate.makeRestCall(request);
  } catch (AssertionError | JsonProcessingException e) {
    var msg = e.getMessage();
    assertTrue(msg.contains("No value at JSON path"));
  }
于 2020-10-16T11:16:49.130 回答
0

doesNotHaveJsonPath用于检查它是否不在 json 正文中

于 2020-05-21T14:34:00.973 回答