org.springframework.test.util.AssertionErrors Java Examples

The following examples show how to use org.springframework.test.util.AssertionErrors. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HeaderAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Expect that the header with the given name is not present.
 */
public WebTestClient.ResponseSpec doesNotExist(String name) {
	if (getHeaders().containsKey(name)) {
		String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]";
		this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
	}
	return this.responseSpec;
}
 
Example #2
Source File: HeaderAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Match the primary value of the response header with a regex.
 * @param name the header name
 * @param pattern the regex pattern
 */
public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
	String value = getRequiredValue(name);
	String message = getMessage(name) + "=[" + value + "] does not match [" + pattern + "]";
	this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertTrue(message, value.matches(pattern)));
	return this.responseSpec;
}
 
Example #3
Source File: HeaderAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
private String getRequiredValue(String name) {
	String value = getHeaders().getFirst(name);
	if (value == null) {
		AssertionErrors.fail(getMessage(name) + " not found");
	}
	return value;
}
 
Example #4
Source File: DefaultWebTestClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> doesNotContain(E... elements) {
	List<E> expected = Arrays.asList(elements);
	List<E> actual = getResult().getResponseBody();
	String message = "Response body should not have contained " + expected;
	getResult().assertWithDiagnostics(() ->
			AssertionErrors.assertTrue(message, (actual == null || !actual.containsAll(expected))));
	return this;
}
 
Example #5
Source File: DefaultWebTestClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> contains(E... elements) {
	List<E> expected = Arrays.asList(elements);
	List<E> actual = getResult().getResponseBody();
	String message = "Response body does not contain " + expected;
	getResult().assertWithDiagnostics(() ->
			AssertionErrors.assertTrue(message, (actual != null && actual.containsAll(expected))));
	return this;
}
 
Example #6
Source File: DefaultWebTestClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ListBodySpec<E> hasSize(int size) {
	List<E> actual = getResult().getResponseBody();
	String message = "Response body does not contain " + size + " elements";
	getResult().assertWithDiagnostics(() ->
			AssertionErrors.assertEquals(message, size, (actual != null ? actual.size() : 0)));
	return this;
}
 
Example #7
Source File: HeaderAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Expect that the header with the given name is present.
 * @since 5.0.3
 */
public WebTestClient.ResponseSpec exists(String name) {
	if (!getHeaders().containsKey(name)) {
		String message = getMessage(name) + " does not exist";
		this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
	}
	return this.responseSpec;
}
 
Example #8
Source File: StatusAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
	HttpStatus status = this.exchangeResult.getStatus();
	this.exchangeResult.assertWithDiagnostics(() -> {
		String message = "Range for response status value " + status;
		AssertionErrors.assertEquals(message, expected, status.series());
	});
	return this.responseSpec;
}
 
Example #9
Source File: HeaderAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Expect that the header with the given name is not present.
 */
public WebTestClient.ResponseSpec doesNotExist(String name) {
	if (getHeaders().containsKey(name)) {
		String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]";
		this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
	}
	return this.responseSpec;
}
 
Example #10
Source File: StatusAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Assert the response error message.
 */
public WebTestClient.ResponseSpec reasonEquals(String reason) {
	String actual = this.exchangeResult.getStatus().getReasonPhrase();
	String message = "Response status reason";
	this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals(message, reason, actual));
	return this.responseSpec;
}
 
Example #11
Source File: CookieResultMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static Cookie getCookie(MvcResult result, String name) {
	Cookie cookie = result.getResponse().getCookie(name);
	if (cookie == null) {
		AssertionErrors.fail("No cookie with name '" + name + "'");
	}
	return cookie;
}
 
Example #12
Source File: CookieResultMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static Cookie getCookie(MvcResult result, String name) {
	Cookie cookie = result.getResponse().getCookie(name);
	if (cookie == null) {
		AssertionErrors.fail("No cookie with name '" + name + "'");
	}
	return cookie;
}
 
Example #13
Source File: HeaderAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private WebTestClient.ResponseSpec assertHeader(String name, @Nullable Object expected, @Nullable Object actual) {
	this.exchangeResult.assertWithDiagnostics(() -> {
		String message = getMessage(name);
		AssertionErrors.assertEquals(message, expected, actual);
	});
	return this.responseSpec;
}
 
Example #14
Source File: HeaderAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Expect a "Content-Type" header compatible with the given value.
 */
public WebTestClient.ResponseSpec contentTypeCompatibleWith(MediaType mediaType) {
	MediaType actual = getHeaders().getContentType();
	String message = getMessage("Content-Type") + "=[" + actual + "] is not compatible with [" + mediaType + "]";
	this.exchangeResult.assertWithDiagnostics(() ->
			AssertionErrors.assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType))));
	return this.responseSpec;
}
 
Example #15
Source File: HeaderAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Expect that the header with the given name is present.
 * @since 5.0.3
 */
public WebTestClient.ResponseSpec exists(String name) {
	if (!getHeaders().containsKey(name)) {
		String message = getMessage(name) + " does not exist";
		this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message));
	}
	return this.responseSpec;
}
 
Example #16
Source File: HeaderAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private String getRequiredValue(String name) {
	String value = getHeaders().getFirst(name);
	if (value == null) {
		AssertionErrors.fail(getMessage(name) + " not found");
	}
	return value;
}
 
Example #17
Source File: HeaderAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Match the primary value of the response header with a regex.
 * @param name the header name
 * @param pattern the regex pattern
 */
public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
	String value = getRequiredValue(name);
	String message = getMessage(name) + "=[" + value + "] does not match [" + pattern + "]";
	this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertTrue(message, value.matches(pattern)));
	return this.responseSpec;
}
 
Example #18
Source File: HeaderAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Expect a "Content-Type" header compatible with the given value.
 */
public WebTestClient.ResponseSpec contentTypeCompatibleWith(MediaType mediaType) {
	MediaType actual = getHeaders().getContentType();
	String message = getMessage("Content-Type") + "=[" + actual + "] is not compatible with [" + mediaType + "]";
	this.exchangeResult.assertWithDiagnostics(() ->
			AssertionErrors.assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType))));
	return this.responseSpec;
}
 
Example #19
Source File: DefaultWebTestClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> doesNotContain(E... elements) {
	List<E> expected = Arrays.asList(elements);
	List<E> actual = getResult().getResponseBody();
	String message = "Response body should not have contained " + expected;
	getResult().assertWithDiagnostics(() ->
			AssertionErrors.assertTrue(message, (actual == null || !actual.containsAll(expected))));
	return this;
}
 
Example #20
Source File: DefaultWebTestClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> contains(E... elements) {
	List<E> expected = Arrays.asList(elements);
	List<E> actual = getResult().getResponseBody();
	String message = "Response body does not contain " + expected;
	getResult().assertWithDiagnostics(() ->
			AssertionErrors.assertTrue(message, (actual != null && actual.containsAll(expected))));
	return this;
}
 
Example #21
Source File: DefaultWebTestClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public ListBodySpec<E> hasSize(int size) {
	List<E> actual = getResult().getResponseBody();
	String message = "Response body does not contain " + size + " elements";
	getResult().assertWithDiagnostics(() ->
			AssertionErrors.assertEquals(message, size, (actual != null ? actual.size() : 0)));
	return this;
}
 
Example #22
Source File: HeaderAssertions.java    From java-technology-stack with MIT License 5 votes vote down vote up
private WebTestClient.ResponseSpec assertHeader(String name, @Nullable Object expected, @Nullable Object actual) {
	this.exchangeResult.assertWithDiagnostics(() -> {
		String message = getMessage(name);
		AssertionErrors.assertEquals(message, expected, actual);
	});
	return this.responseSpec;
}
 
Example #23
Source File: StatusAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
	HttpStatus status = this.exchangeResult.getStatus();
	this.exchangeResult.assertWithDiagnostics(() -> {
		String message = "Range for response status value " + status;
		AssertionErrors.assertEquals(message, expected, status.series());
	});
	return this.responseSpec;
}
 
Example #24
Source File: MockRestRequestMatchers.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Expect a request to the given URI.
 * @param uri the expected URI
 * @return the request matcher
 */
public static RequestMatcher requestTo(final URI uri) {
	Assert.notNull(uri, "'uri' must not be null");
	return new RequestMatcher() {
		@Override
		public void match(ClientHttpRequest request) throws IOException, AssertionError {
			AssertionErrors.assertEquals("Unexpected request", uri, request.getURI());
		}
	};
}
 
Example #25
Source File: StatusAssertions.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Assert the response error message.
 */
public WebTestClient.ResponseSpec reasonEquals(String reason) {
	String actual = this.exchangeResult.getStatus().getReasonPhrase();
	String message = "Response status reason";
	this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals(message, reason, actual));
	return this.responseSpec;
}
 
Example #26
Source File: StatusAssertions.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Assert the response status as an integer.
 */
public WebTestClient.ResponseSpec isEqualTo(int status) {
	int actual = this.exchangeResult.getStatus().value();
	this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals("Status", status, actual));
	return this.responseSpec;
}
 
Example #27
Source File: MockRestRequestMatchers.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private static void assertHeaderValueCount(final String name, HttpHeaders headers, int expectedCount) {
	List<String> actualValues = headers.get(name);
	AssertionErrors.assertTrue("Expected header <" + name + ">", actualValues != null);
	AssertionErrors.assertTrue("Expected header <" + name + "> to have at least <" + expectedCount
			+ "> values but found " + actualValues, expectedCount <= actualValues.size());
}
 
Example #28
Source File: DefaultWebTestClient.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public EntityExchangeResult<Void> isEmpty() {
	this.result.assertWithDiagnostics(() ->
			AssertionErrors.assertTrue("Expected empty body", this.isEmpty));
	return new EntityExchangeResult<>(this.result, null);
}
 
Example #29
Source File: DefaultWebTestClient.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public <T extends S> T isEqualTo(B expected) {
	this.result.assertWithDiagnostics(() ->
			AssertionErrors.assertEquals("Response body", expected, this.result.getResponseBody()));
	return self();
}
 
Example #30
Source File: StatusAssertions.java    From java-technology-stack with MIT License 4 votes vote down vote up
private WebTestClient.ResponseSpec assertStatusAndReturn(HttpStatus expected) {
	HttpStatus actual = this.exchangeResult.getStatus();
	this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals("Status", expected, actual));
	return this.responseSpec;
}