Migrates web examples to JUnit 5.

Original pull request #602
Related tickest #583
This commit is contained in:
divya_jnu08
2021-01-26 23:13:46 +05:30
committed by Jens Schauder
parent 64c1eeaf05
commit 9db9632c4b
7 changed files with 34 additions and 26 deletions

View File

@@ -273,14 +273,19 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>

View File

@@ -15,15 +15,15 @@
*/
package example.users;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Integration tests to bootstrap the application.
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
public abstract class AbstractIntegrationTests {}

View File

@@ -16,9 +16,9 @@
package example.users;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**

View File

@@ -15,7 +15,8 @@
*/
package example.users;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -31,8 +32,10 @@ public class UserRepositoryIntegrationTests extends AbstractIntegrationTests {
/**
* @see #65
*/
@Test(expected = InvalidDataAccessApiUsageException.class)
@Test
public void repositoryRejectsUnencryptedPassword() {
users.save(new User(new Username("olivergierke"), Password.raw("foobar")));
Assertions.assertThrows(InvalidDataAccessApiUsageException.class, () -> {
users.save(new User(new Username("olivergierke"), Password.raw("foobar")));
});
}
}

View File

@@ -16,12 +16,12 @@
package example.users;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.*;
import example.users.UserController.UserPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@@ -37,7 +37,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Integration tests for {@link UserController} to demonstrate client-side resilience of the payload type against
@@ -45,7 +45,7 @@ import org.springframework.test.context.junit4.SpringRunner;
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class UserControllerClientTests {

View File

@@ -19,14 +19,14 @@ import static org.hamcrest.CoreMatchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
@@ -35,7 +35,7 @@ import org.springframework.test.web.servlet.ResultActions;
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc(print = MockMvcPrint.NONE)
public class UserControllerIntegrationTests {
@@ -61,10 +61,9 @@ public class UserControllerIntegrationTests {
private void postAndExpect(String payload, MediaType mediaType) throws Exception {
ResultActions actions = mvc
.perform(post("/")//
.content(payload)//
.contentType(mediaType))//
ResultActions actions = mvc.perform(post("/")//
.content(payload)//
.contentType(mediaType))//
.andExpect(status().isOk());
actions.andExpect(content().string(containsString("Dave")));

View File

@@ -15,15 +15,16 @@
*/
package example.users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ApplicationTests {