Upgrade tests to Junit5
See gh-14737
This commit is contained in:
@@ -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,16 +27,16 @@ 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;
|
||||
|
||||
/**
|
||||
* Basic integration tests for demo application.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
|
||||
@SpringBootTest(classes = { SampleSecureApplication.class })
|
||||
public class SampleSecureApplicationTests {
|
||||
|
||||
@@ -46,19 +45,20 @@ public class SampleSecureApplicationTests {
|
||||
|
||||
private Authentication authentication;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
this.authentication = new UsernamePasswordAuthenticationToken("user", "password");
|
||||
}
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void close() {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationException.class)
|
||||
@Test
|
||||
public void secure() {
|
||||
assertThat("Hello Security").isEqualTo(this.service.secure());
|
||||
assertThatExceptionOfType(AuthenticationException.class)
|
||||
.isThrownBy(() -> SampleSecureApplicationTests.this.service.secure());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -73,10 +73,11 @@ public class SampleSecureApplicationTests {
|
||||
assertThat("Hello World").isEqualTo(this.service.authorized());
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@Test
|
||||
public void denied() {
|
||||
SecurityContextHolder.getContext().setAuthentication(this.authentication);
|
||||
assertThat("Goodbye World").isEqualTo(this.service.denied());
|
||||
assertThatExceptionOfType(AccessDeniedException.class)
|
||||
.isThrownBy(() -> SampleSecureApplicationTests.this.service.denied());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user