Skip to content

Instantly share code, notes, and snippets.

@abhi2495
Last active July 19, 2020 20:31
Show Gist options
  • Save abhi2495/b17c0e0f5d74f1cdcdeadbd1f9aefc90 to your computer and use it in GitHub Desktop.
Save abhi2495/b17c0e0f5d74f1cdcdeadbd1f9aefc90 to your computer and use it in GitHub Desktop.
/*
##################################################################################
##################################################################################
######### IF YOU FOUND THIS GIST USEFUL, PLEASE LEAVE A STAR. THANKS. ############
##################################################################################
##################################################################################
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles(Application.Profiles.TEST)
@AutoConfigureWebTestClient(timeout = 60000) //in milliseconds
@AutoConfigureWireMock(port = 0) //random port
public class GreetingControllerIntegrationTest {
@Autowired
private WebTestClient webTestClient;
@Test
public void test_Greet() throws JsonProcessingException {
TestUtil.mockOAuthClientTokenResponse();
Tenant tenant = new Tenant("x-foo", "foo");
stubFor(get(urlEqualTo("/tenantApi?alias=foo"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.withBody(new ObjectMapper().writeValueAsString(Collections.singletonList(tenant)))));
webTestClient
.mutateWith(mockOidcLogin().authorities(new SimpleGrantedAuthority("SCOPE_greet")))
.get()
.uri(.....)
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo(....);
}
}

A sample Integration Test for a reactive webapplication written using Spring WebFlux

public final class TestUtil {
private static final String MOCK_TOKEN_RESPONSE = "{\n" +
" \"access_token\": \"eyJhbG\",\n" +
" \"expires_in\": 600,\n" +
" \"refresh_expires_in\": 1800,\n" +
" \"refresh_token\": \"eyJhbGc\",\n" +
" \"token_type\": \"bearer\",\n" +
" \"not-before-policy\": 0,\n" +
" \"session_state\": \"9de04c8c-1790-4336-b77a-b9b14c20d7c3\",\n" +
" \"scope\": \"greet\"\n" +
"}";
private TestUtil() {
}
public static void mockOAuthClientTokenResponse() {
stubFor(post(urlEqualTo("/.well-known/token"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.withBody(MOCK_TOKEN_RESPONSE)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment