java-tutorials/spring-5/src/test/java/com/baeldung/security/SecurityIntegrationTest.java

39 lines
1.2 KiB
Java
Raw Normal View History

package com.baeldung.security;
import com.baeldung.SpringSecurity5Application;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = SpringSecurity5Application.class)
2018-03-04 17:52:56 +02:00
public class SecurityIntegrationTest {
@Autowired
ApplicationContext context;
private WebTestClient rest;
@Before
public void setup() {
2018-03-04 17:52:56 +02:00
this.rest = WebTestClient.bindToApplicationContext(this.context).configureClient().build();
}
@Test
public void whenNoCredentials_thenRedirectToLogin() {
2018-03-04 17:52:56 +02:00
this.rest.get().uri("/").exchange().expectStatus().is3xxRedirection();
}
@Test
@WithMockUser
public void whenHasCredentials_thenSeesGreeting() {
2018-03-04 17:52:56 +02:00
this.rest.get().uri("/").exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello, user");
}
}