Validate our own tests work with JUnit5 and the vintage engine

Closes gh-14737

Co-authored-by: Stephane Nicoll <snicoll@pivotal.io>
This commit is contained in:
Madhura Bhave
2018-12-05 13:56:29 -08:00
committed by Stephane Nicoll
parent d9f339a1b6
commit 1db1c8b03c
821 changed files with 2279 additions and 2787 deletions

View File

@@ -16,10 +16,9 @@
package sample.secure;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -28,7 +27,6 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -38,45 +36,44 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { SampleSecureApplication.class })
public class SampleSecureApplicationTests {
class SampleSecureApplicationTests {
@Autowired
private SampleService service;
private Authentication authentication;
@Before
@BeforeEach
public void init() {
this.authentication = new UsernamePasswordAuthenticationToken("user", "password");
}
@After
@AfterEach
public void close() {
SecurityContextHolder.clearContext();
}
@Test
public void secure() {
void secure() {
assertThatExceptionOfType(AuthenticationException.class)
.isThrownBy(() -> SampleSecureApplicationTests.this.service.secure());
}
@Test
public void authenticated() {
void authenticated() {
SecurityContextHolder.getContext().setAuthentication(this.authentication);
assertThat("Hello Security").isEqualTo(this.service.secure());
}
@Test
public void preauth() {
void preauth() {
SecurityContextHolder.getContext().setAuthentication(this.authentication);
assertThat("Hello World").isEqualTo(this.service.authorized());
}
@Test
public void denied() {
void denied() {
SecurityContextHolder.getContext().setAuthentication(this.authentication);
assertThatExceptionOfType(AccessDeniedException.class)
.isThrownBy(() -> SampleSecureApplicationTests.this.service.denied());