Skip to content

Commit

Permalink
Migrate to Spring 6.2's AssertJ based MVC assertions.
Browse files Browse the repository at this point in the history
Co-authored-by: Oliver Drotbohm <[email protected]>
  • Loading branch information
snicoll and odrotbohm committed Dec 10, 2024
1 parent ca49d92 commit d215c68
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,26 +15,23 @@
*/
package org.springsource.restbucks;

import static org.assertj.core.api.Assertions.*;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

import java.util.Locale;
import java.util.Optional;

import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import org.springframework.hateoas.client.LinkDiscoverer;
import org.springframework.hateoas.client.LinkDiscoverers;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.assertj.MockMvcTester;
import org.springframework.test.web.servlet.assertj.MvcTestResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;

/**
Expand All @@ -48,66 +45,43 @@ public abstract class AbstractWebIntegrationTest {
@Autowired WebApplicationContext context;
@Autowired LinkDiscoverers links;

protected MockMvc mvc;
protected MockMvcTester mvc;

@BeforeEach
void setUp() {

mvc = MockMvcBuilders.webAppContextSetup(context).//
defaultRequest(MockMvcRequestBuilders.get("/").locale(Locale.US)).//
build();
this.mvc = MockMvcTester.from(context, builder ->//
builder.defaultRequest(MockMvcRequestBuilders.get("/").locale(Locale.US)).//
build());
}

/**
* Creates a {@link ResultMatcher} that checks for the presence of a link with the given rel.
* Creates a AssertJ {@link Condition} that checks for the presence of a {@link Link} with the given
* {@link LinkRelation}.
*
* @param rel
* @return
* @param rel must not be {@literal null}.
* @return will never be {@literal null}.
*/
protected ResultMatcher linkWithRelIsPresent(LinkRelation rel) {
return new LinkWithRelMatcher(rel, true);
}
protected Condition<MvcTestResult> linkWithRel(LinkRelation rel) {

/**
* Creates a {@link ResultMatcher} that checks for the non-presence of a link with the given rel.
*
* @param rel
* @return
*/
protected ResultMatcher linkWithRelIsNotPresent(LinkRelation rel) {
return new LinkWithRelMatcher(rel, false);
Assert.notNull(rel, "LinkRelation must not be null!");

return new Condition<>(it -> hasLink(it.getMvcResult(), rel), "Expected to find link with relation %s!", rel);
}

@SuppressWarnings("null")
protected LinkDiscoverer getDiscovererFor(MockHttpServletResponse response) {
return links.getRequiredLinkDiscovererFor(response.getContentType());
}

@RequiredArgsConstructor
private class LinkWithRelMatcher implements ResultMatcher {

private final LinkRelation rel;
private final boolean present;

/*
* (non-Javadoc)
* @see org.springframework.test.web.servlet.ResultMatcher#match(org.springframework.test.web.servlet.MvcResult)
*/
@Override
public void match(MvcResult result) throws Exception {

MockHttpServletResponse response = result.getResponse();
String content = response.getContentAsString();
LinkDiscoverer discoverer = links.getRequiredLinkDiscovererFor(response.getContentType());

Optional<Link> link = discoverer.findLinkWithRel(rel, content);

assertThat(link).matches(it -> it.isPresent() == present, getMessage(link));
}
@SneakyThrows
@SuppressWarnings("null")
private boolean hasLink(MvcResult result, LinkRelation rel) {

private String getMessage(Optional<Link> link) {
var response = result.getResponse();
var content = response.getContentAsString();
var discoverer = links.getRequiredLinkDiscovererFor(response.getContentType());

return String.format("Expected to %s link with relation %s, but found %s!",
present ? "find" : "not find", rel, present ? link.get() : "none");
}
return discoverer.findLinkWithRel(rel, content).isPresent();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,12 +15,12 @@
*/
package org.springsource.restbucks.order.web;

import static org.hamcrest.CoreMatchers.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import org.junit.jupiter.api.Test;
import org.springframework.hateoas.MediaTypes;
import org.springframework.http.HttpStatus;
import org.springsource.restbucks.AbstractWebIntegrationTest;

/**
Expand All @@ -33,9 +33,10 @@ class OrderResourceIntegrationTest extends AbstractWebIntegrationTest {
@Test
void exposesOrdersResourceViaRootResource() throws Exception {

mvc.perform(get("/")) //
.andExpect(status().isOk()) //
.andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON)) //
.andExpect(jsonPath("$._links.restbucks:orders.href", notNullValue()));
var result = mvc.perform(get("/")); //

assertThat(result).hasStatus(HttpStatus.OK);
assertThat(result).contentType().isCompatibleWith(MediaTypes.HAL_JSON);
assertThat(result).bodyJson().hasPath("$._links.restbucks:orders.href").isNotNull();
}
}
Loading

0 comments on commit d215c68

Please sign in to comment.