org.assertj.core.api.AbstractFileAssert.has()方法的使用及代码示例

x33g5p2x  于11个月前 转载在 其他  
字(16.5k)|赞(0)|评价(0)|浏览(34)

本文整理了Java中org.assertj.core.api.AbstractFileAssert.has()方法的一些代码示例,展示了AbstractFileAssert.has()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AbstractFileAssert.has()方法的具体详情如下:
包路径:org.assertj.core.api.AbstractFileAssert
类名称:AbstractFileAssert
方法名:has

AbstractFileAssert.has介绍

[英]Verifies that the binary content of the actual File is exactly equal to the given one.

Example:

File bin = File.createTempFile("tmp", "bin"); 
Files.write(bin.toPath(), new byte[] {1, 1}); 
// assertion will pass 
assertThat(bin).hasBinaryContent(new byte[] {1, 1}); 
// assertions will fail 
assertThat(bin).hasBinaryContent(new byte[] { }); 
assertThat(bin).hasBinaryContent(new byte[] {0, 0});

[中]验证实际文件的二进制内容是否与给定文件完全相同。
例子:

File bin = File.createTempFile("tmp", "bin"); 
Files.write(bin.toPath(), new byte[] {1, 1}); 
// assertion will pass 
assertThat(bin).hasBinaryContent(new byte[] {1, 1}); 
// assertions will fail 
assertThat(bin).hasBinaryContent(new byte[] { }); 
assertThat(bin).hasBinaryContent(new byte[] {0, 0});

代码示例

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithCookies() throws Exception {
  this.webTestClient.get().uri("/").cookie("cookieName", "cookieVal")
      .accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
      .expectBody().consumeWith(document("curl-snippet-with-cookies"));
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format(
                  "$ curl 'https://api.example.com/' -i -X GET \\%n"
                      + "    -H 'Accept: application/json' \\%n"
                      + "    --cookie 'cookieName=cookieVal'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void httpieSnippetWithCookies() throws Exception {
  this.webTestClient.get().uri("/").cookie("cookieName", "cookieVal")
      .accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk()
      .expectBody().consumeWith(document("httpie-snippet-with-cookies"));
  assertThat(new File(
      "build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format(
                  "$ http GET 'https://api.example.com/' \\%n"
                      + "    'Accept:application/json' \\%n"
                      + "    'Cookie:cookieName=cookieVal'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void responseWithSetCookie() throws Exception {
  this.webTestClient.get().uri("/set-cookie").exchange().expectStatus().isOk()
      .expectBody().consumeWith(document("set-cookie"));
  assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"))
      .has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
          .header(HttpHeaders.SET_COOKIE,
              "name=value; Domain=localhost; HttpOnly")));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void httpieSnippetWithCookies() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)
      .cookie(new Cookie("cookieName", "cookieVal"))).andExpect(status().isOk())
      .andDo(document("httpie-snippet-with-cookies"));
  assertThat(new File(
      "build/generated-snippets/httpie-snippet-with-cookies/httpie-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String
                  .format("$ http GET 'http://localhost:8080/' \\%n"
                      + "    'Accept:application/json' \\%n"
                      + "    'Cookie:cookieName=cookieVal'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithCookies() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)
      .cookie(new Cookie("cookieName", "cookieVal"))).andExpect(status().isOk())
      .andDo(document("curl-snippet-with-cookies"));
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format(
                  "$ curl 'http://localhost:8080/' -i -X GET \\%n"
                      + "    -H 'Accept: application/json' \\%n"
                      + "    --cookie 'cookieName=cookieVal'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void multipart() throws Exception {
  MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();
  multipartData.add("a", "alpha");
  multipartData.add("b", "bravo");
  Consumer<EntityExchangeResult<byte[]>> documentation = document("multipart",
      requestParts(partWithName("a").description("Part a"),
          partWithName("b").description("Part b")));
  this.webTestClient.post().uri("/upload")
      .body(BodyInserters.fromMultipartData(multipartData)).exchange()
      .expectStatus().isOk().expectBody().consumeWith(documentation);
  assertThat(new File("build/generated-snippets/multipart/request-parts.adoc"))
      .has(content(tableWithHeader(TemplateFormats.asciidoctor(), "Part",
          "Description").row("`a`", "Part a").row("`b`", "Part b")));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void customContextPath() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(
      get("/custom/").contextPath("/custom").accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isOk()).andDo(document("custom-context-path"));
  assertThat(new File(
      "build/generated-snippets/custom-context-path/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format(
                  "$ curl 'http://localhost:8080/custom/' -i -X GET \\%n"
                      + "    -H 'Accept: application/json'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithContent() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content"))
      .andExpect(status().isOk()).andDo(document("curl-snippet-with-content"));
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-content/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format(
                  "$ curl 'http://localhost:8080/' -i -X POST \\%n"
                      + "    -H 'Accept: application/json' \\%n"
                      + "    -d 'content'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void httpieSnippetWithContent() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(post("/").accept(MediaType.APPLICATION_JSON).content("content"))
      .andExpect(status().isOk())
      .andDo(document("httpie-snippet-with-content"));
  assertThat(new File(
      "build/generated-snippets/httpie-snippet-with-content/httpie-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format("$ echo 'content' | "
                  + "http POST 'http://localhost:8080/' \\%n"
                  + "    'Accept:application/json'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void responseWithCookie() {
  given().port(tomcat.getPort())
      .filter(documentationConfiguration(this.restDocumentation))
      .filter(document("set-cookie",
          preprocessResponse(removeHeaders(HttpHeaders.DATE,
              HttpHeaders.CONTENT_TYPE))))
      .get("/set-cookie").then().statusCode(200);
  assertExpectedSnippetFilesExist(new File("build/generated-snippets/set-cookie"),
      "http-request.adoc", "http-response.adoc", "curl-request.adoc");
  assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"))
      .has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
          .header(HttpHeaders.SET_COOKIE,
              "name=value; Domain=localhost; HttpOnly")));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithContentAndParametersOnPost() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(post("/").param("a", "alpha").accept(MediaType.APPLICATION_JSON)
      .content("some content")).andExpect(status().isOk())
      .andDo(document("curl-snippet-with-content-and-parameters"));
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-content-and-parameters/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format(
                  "$ curl 'http://localhost:8080/?a=alpha' -i -X POST \\%n"
                      + "    -H 'Accept: application/json' \\%n"
                      + "    -d 'some content'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void httpieSnippetWithQueryStringOnPost() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(post("/?foo=bar").param("foo", "bar").param("a", "alpha")
      .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
      .andDo(document("httpie-snippet-with-query-string"));
  assertThat(new File(
      "build/generated-snippets/httpie-snippet-with-query-string/httpie-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format("$ http "
                  + "--form POST 'http://localhost:8080/?foo=bar' \\%n"
                  + "    'Accept:application/json' \\%n    'a=alpha'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void httpieSnippetWithContentAndParametersOnPost() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(post("/").param("a", "alpha").content("some content")
      .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
      .andDo(document("httpie-snippet-post-with-content-and-parameters"));
  assertThat(new File(
      "build/generated-snippets/httpie-snippet-post-with-content-and-parameters/httpie-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String
                  .format("$ echo " + "'some content' | http POST "
                      + "'http://localhost:8080/?a=alpha' \\%n"
                      + "    'Accept:application/json'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithQueryStringOnPost() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(post("/?foo=bar").param("foo", "bar").param("a", "alpha")
      .accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
      .andDo(document("curl-snippet-with-query-string"));
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format("$ curl "
                  + "'http://localhost:8080/?foo=bar' -i -X POST \\%n"
                  + "    -H 'Accept: application/json' \\%n"
                  + "    -d 'a=alpha'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithContent() throws Exception {
  String contentType = "text/plain; charset=UTF-8";
  given().port(tomcat.getPort())
      .filter(documentationConfiguration(this.restDocumentation))
      .filter(document("curl-snippet-with-content")).accept("application/json")
      .body("content").contentType(contentType).post("/").then()
      .statusCode(200);
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-content/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format("$ curl 'http://localhost:"
                  + tomcat.getPort() + "/' -i -X POST \\%n"
                  + "    -H 'Accept: application/json' \\%n"
                  + "    -H 'Content-Type: " + contentType
                  + "' \\%n" + "    -d 'content'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithCookies() throws Exception {
  String contentType = "text/plain; charset=UTF-8";
  given().port(tomcat.getPort())
      .filter(documentationConfiguration(this.restDocumentation))
      .filter(document("curl-snippet-with-cookies")).accept("application/json")
      .contentType(contentType).cookie("cookieName", "cookieVal").get("/")
      .then().statusCode(200);
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-cookies/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format("$ curl 'http://localhost:"
                  + tomcat.getPort() + "/' -i -X GET \\%n"
                  + "    -H 'Accept: application/json' \\%n"
                  + "    -H 'Content-Type: " + contentType
                  + "' \\%n"
                  + "    --cookie 'cookieName=cookieVal'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void curlSnippetWithQueryStringOnPost() throws Exception {
  given().port(tomcat.getPort())
      .filter(documentationConfiguration(this.restDocumentation))
      .filter(document("curl-snippet-with-query-string"))
      .accept("application/json").param("foo", "bar").param("a", "alpha")
      .post("/?foo=bar").then().statusCode(200);
  String contentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";
  assertThat(new File(
      "build/generated-snippets/curl-snippet-with-query-string/curl-request.adoc"))
          .has(content(codeBlock(TemplateFormats.asciidoctor(), "bash")
              .withContent(String.format("$ curl "
                  + "'http://localhost:" + tomcat.getPort()
                  + "/?foo=bar' -i -X POST \\%n"
                  + "    -H 'Accept: application/json' \\%n"
                  + "    -H 'Content-Type: " + contentType
                  + "' \\%n" + "    -d 'a=alpha'"))));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void responseWithSetCookie() throws Exception {
  MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
      .apply(documentationConfiguration(this.restDocumentation)).build();
  mockMvc.perform(get("/set-cookie")).andExpect(status().isOk())
      .andDo(document("set-cookie",
          responseHeaders(headerWithName(HttpHeaders.SET_COOKIE)
              .description("set-cookie"))));
  assertThat(new File("build/generated-snippets/set-cookie/http-response.adoc"))
      .has(content(httpResponse(TemplateFormats.asciidoctor(), HttpStatus.OK)
          .header(HttpHeaders.SET_COOKIE,
              "name=value; Domain=localhost; HttpOnly")));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void requestParametersSnippet() {
  this.webTestClient.get().uri("/?a=alpha&b=bravo").exchange().expectStatus().isOk()
      .expectBody()
      .consumeWith(document("request-parameters", requestParameters(
          parameterWithName("a").description("Alpha description"),
          parameterWithName("b").description("Bravo description"))));
  assertThat(new File(
      "build/generated-snippets/request-parameters/request-parameters.adoc"))
          .has(content(tableWithHeader(TemplateFormats.asciidoctor(),
              "Parameter", "Description")
                  .row("`a`", "Alpha description")
                  .row("`b`", "Bravo description")));
}

代码示例来源:origin: spring-projects/spring-restdocs

@Test
public void pathParametersSnippet() {
  this.webTestClient.get().uri("/{foo}/{bar}", "1", "2").exchange().expectStatus()
      .isOk().expectBody()
      .consumeWith(document("path-parameters", pathParameters(
          parameterWithName("foo").description("Foo description"),
          parameterWithName("bar").description("Bar description"))));
  assertThat(
      new File("build/generated-snippets/path-parameters/path-parameters.adoc"))
          .has(content(
              tableWithTitleAndHeader(TemplateFormats.asciidoctor(),
                  "+/{foo}/{bar}+", "Parameter", "Description")
                      .row("`foo`", "Foo description")
                      .row("`bar`", "Bar description")));
}

相关文章