use webmvctest
This commit is contained in:
parent
49030da9ca
commit
055dfcab13
@ -41,6 +41,8 @@ dependencies {
|
|||||||
compileOnly('org.projectlombok:lombok')
|
compileOnly('org.projectlombok:lombok')
|
||||||
runtime('com.h2database:h2')
|
runtime('com.h2database:h2')
|
||||||
testCompile 'io.rest-assured:rest-assured:3.0.2'
|
testCompile 'io.rest-assured:rest-assured:3.0.2'
|
||||||
|
testCompile 'io.rest-assured:spring-mock-mvc:3.0.2'
|
||||||
|
testCompile 'org.springframework.security:spring-security-test:4.0.4.RELEASE'
|
||||||
testCompile('org.springframework.boot:spring-boot-starter-test')
|
testCompile('org.springframework.boot:spring-boot-starter-test')
|
||||||
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
|
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
|
||||||
testCompile('org.mybatis.spring.boot:mybatis-spring-boot-starter-test:1.3.0')
|
testCompile('org.mybatis.spring.boot:mybatis-spring-boot-starter-test:1.3.0')
|
||||||
|
13
src/test/java/io/spring/api/ApiTestBase.java
Normal file
13
src/test/java/io/spring/api/ApiTestBase.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package io.spring.api;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
public abstract class ApiTestBase {
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.application.data.ArticleData;
|
import io.spring.JacksonCustomizations;
|
||||||
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
|
import io.spring.application.data.ArticleData;
|
||||||
import io.spring.application.data.ProfileData;
|
import io.spring.application.data.ProfileData;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
@ -12,25 +14,28 @@ import io.spring.core.favorite.ArticleFavoriteRepository;
|
|||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@WebMvcTest(ArticleFavoriteApi.class)
|
||||||
@RunWith(SpringRunner.class)
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private ArticleFavoriteRepository articleFavoriteRepository;
|
private ArticleFavoriteRepository articleFavoriteRepository;
|
||||||
|
|
||||||
@ -40,22 +45,13 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
|
|||||||
@MockBean
|
@MockBean
|
||||||
private ArticleQueryService articleQueryService;
|
private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
protected String email;
|
|
||||||
protected String username;
|
|
||||||
protected String defaultAvatar;
|
|
||||||
|
|
||||||
@LocalServerPort
|
|
||||||
private int port;
|
|
||||||
private Article article;
|
private Article article;
|
||||||
private User anotherUser;
|
private User anotherUser;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
super.setUp();
|
||||||
email = "john@jacob.com";
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
username = "johnjacob";
|
|
||||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
|
||||||
userFixture();
|
|
||||||
anotherUser = new User("other@test.com", "other", "123", "", "");
|
anotherUser = new User("other@test.com", "other", "123", "", "");
|
||||||
article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId());
|
article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId());
|
||||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
|
import io.spring.JacksonCustomizations;
|
||||||
import io.spring.TestHelper;
|
import io.spring.TestHelper;
|
||||||
import io.spring.application.data.ArticleData;
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.application.ArticleQueryService;
|
||||||
|
import io.spring.application.data.ArticleData;
|
||||||
import io.spring.application.data.ProfileData;
|
import io.spring.application.data.ProfileData;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.article.ArticleRepository;
|
import io.spring.core.article.ArticleRepository;
|
||||||
@ -12,31 +14,30 @@ import org.joda.time.DateTime;
|
|||||||
import org.joda.time.format.ISODateTimeFormat;
|
import org.joda.time.format.ISODateTimeFormat;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@WebMvcTest({ArticlesApi.class, ArticleApi.class})
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class ArticlesApiTest extends TestWithCurrentUser {
|
public class ArticlesApiTest extends TestWithCurrentUser {
|
||||||
@LocalServerPort
|
@Autowired
|
||||||
private int port;
|
private MockMvc mvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private ArticleRepository articleRepository;
|
private ArticleRepository articleRepository;
|
||||||
@ -44,10 +45,11 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
@MockBean
|
@MockBean
|
||||||
private ArticleQueryService articleQueryService;
|
private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
|
@Override
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
super.setUp();
|
||||||
userFixture();
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -122,7 +124,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
|
when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
|
||||||
|
|
||||||
RestAssured.when()
|
RestAssuredMockMvc.when()
|
||||||
.get("/articles/{slug}", slug)
|
.get("/articles/{slug}", slug)
|
||||||
.then()
|
.then()
|
||||||
.statusCode(200)
|
.statusCode(200)
|
||||||
@ -135,7 +137,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
|
|||||||
@Test
|
@Test
|
||||||
public void should_404_if_article_not_found() throws Exception {
|
public void should_404_if_article_not_found() throws Exception {
|
||||||
when(articleQueryService.findBySlug(anyString(), any())).thenReturn(Optional.empty());
|
when(articleQueryService.findBySlug(anyString(), any())).thenReturn(Optional.empty());
|
||||||
RestAssured.when()
|
RestAssuredMockMvc.when()
|
||||||
.get("/articles/not-exists")
|
.get("/articles/not-exists")
|
||||||
.then()
|
.then()
|
||||||
.statusCode(404);
|
.statusCode(404);
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
|
import io.spring.JacksonCustomizations;
|
||||||
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.CommentQueryService;
|
import io.spring.application.CommentQueryService;
|
||||||
import io.spring.application.data.CommentData;
|
import io.spring.application.data.CommentData;
|
||||||
import io.spring.application.data.ProfileData;
|
import io.spring.application.data.ProfileData;
|
||||||
@ -11,33 +13,27 @@ import io.spring.core.comment.CommentRepository;
|
|||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
@WebMvcTest(CommentsApi.class)
|
||||||
@RunWith(SpringRunner.class)
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class CommentsApiTest extends TestWithCurrentUser {
|
public class CommentsApiTest extends TestWithCurrentUser {
|
||||||
@LocalServerPort
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
protected String email;
|
|
||||||
protected String username;
|
|
||||||
protected String defaultAvatar;
|
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private ArticleRepository articleRepository;
|
private ArticleRepository articleRepository;
|
||||||
@ -50,15 +46,13 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
|||||||
private Article article;
|
private Article article;
|
||||||
private CommentData commentData;
|
private CommentData commentData;
|
||||||
private Comment comment;
|
private Comment comment;
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mvc;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
email = "john@jacob.com";
|
super.setUp();
|
||||||
username = "johnjacob";
|
|
||||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
|
||||||
userFixture();
|
|
||||||
|
|
||||||
article = new Article("title", "desc", "body", new String[]{"test", "java"}, user.getId());
|
article = new Article("title", "desc", "body", new String[]{"test", "java"}, user.getId());
|
||||||
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
|
||||||
comment = new Comment("comment", user.getId(), article.getId());
|
comment = new Comment("comment", user.getId(), article.getId());
|
||||||
@ -115,7 +109,7 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
|||||||
@Test
|
@Test
|
||||||
public void should_get_comments_of_article_success() throws Exception {
|
public void should_get_comments_of_article_success() throws Exception {
|
||||||
when(commentQueryService.findByArticleId(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
|
when(commentQueryService.findByArticleId(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
|
||||||
RestAssured.when()
|
RestAssuredMockMvc.when()
|
||||||
.get("/articles/{slug}/comments", article.getSlug())
|
.get("/articles/{slug}/comments", article.getSlug())
|
||||||
.prettyPeek()
|
.prettyPeek()
|
||||||
.then()
|
.then()
|
||||||
@ -139,6 +133,8 @@ public class CommentsApiTest extends TestWithCurrentUser {
|
|||||||
public void should_get_403_if_not_author_of_article_or_author_of_comment_when_delete_comment() throws Exception {
|
public void should_get_403_if_not_author_of_article_or_author_of_comment_when_delete_comment() throws Exception {
|
||||||
User anotherUser = new User("other@example.com", "other", "123", "", "");
|
User anotherUser = new User("other@example.com", "other", "123", "", "");
|
||||||
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
|
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
|
||||||
|
when(jwtService.getSubFromToken(any())).thenReturn(Optional.of(anotherUser.getId()));
|
||||||
|
when(userRepository.findById(eq(anotherUser.getId()))).thenReturn(Optional.ofNullable(anotherUser));
|
||||||
|
|
||||||
when(commentRepository.findById(eq(article.getId()), eq(comment.getId()))).thenReturn(Optional.of(comment));
|
when(commentRepository.findById(eq(article.getId()), eq(comment.getId()))).thenReturn(Optional.of(comment));
|
||||||
String token = jwtService.toToken(anotherUser);
|
String token = jwtService.toToken(anotherUser);
|
||||||
|
@ -1,41 +1,48 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.application.data.UserData;
|
import io.spring.JacksonCustomizations;
|
||||||
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
|
import io.spring.application.UserQueryService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
@WebMvcTest(CurrentUserApi.class)
|
||||||
@RunWith(SpringRunner.class)
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class CurrentUserApiTest extends TestWithCurrentUser {
|
public class CurrentUserApiTest extends TestWithCurrentUser {
|
||||||
|
|
||||||
@LocalServerPort
|
@Autowired
|
||||||
private int port;
|
private MockMvc mvc;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private UserQueryService userQueryService;
|
||||||
|
|
||||||
|
@Override
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
super.setUp();
|
||||||
userFixture();
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_current_user_with_token() throws Exception {
|
public void should_get_current_user_with_token() throws Exception {
|
||||||
|
when(userQueryService.findById(any())).thenReturn(Optional.of(userData));
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.header("Authorization", "Token " + token)
|
.header("Authorization", "Token " + token)
|
||||||
@ -64,9 +71,11 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_401_with_invalid_token() throws Exception {
|
public void should_get_401_with_invalid_token() throws Exception {
|
||||||
|
String invalidToken = "asdfasd";
|
||||||
|
when(jwtService.getSubFromToken(eq(invalidToken))).thenReturn(Optional.empty());
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
.header("Authorization", "Token asdfasd")
|
.header("Authorization", "Token " + invalidToken)
|
||||||
.when()
|
.when()
|
||||||
.get("/user")
|
.get("/user")
|
||||||
.then()
|
.then()
|
||||||
@ -90,7 +99,7 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
|
|||||||
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
|
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
|
||||||
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.empty());
|
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.empty());
|
||||||
|
|
||||||
when(userReadService.findByUsername(eq(newUsername))).thenReturn(new UserData(user.getId(), newEmail, newUsername, newBio, user.getImage()));
|
when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
@ -113,7 +122,7 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
|
|||||||
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.of(new User(newEmail, "username", "123", "", "")));
|
when(userRepository.findByEmail(eq(newEmail))).thenReturn(Optional.of(new User(newEmail, "username", "123", "", "")));
|
||||||
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
|
when(userRepository.findByUsername(eq(newUsername))).thenReturn(Optional.empty());
|
||||||
|
|
||||||
when(userReadService.findByUsername(eq(newUsername))).thenReturn(new UserData(user.getId(), newEmail, newUsername, newBio, user.getImage()));
|
when(userQueryService.findById(eq(user.getId()))).thenReturn(Optional.of(userData));
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
|
@ -1,36 +1,43 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
|
import io.spring.JacksonCustomizations;
|
||||||
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
|
import io.spring.application.ArticleQueryService;
|
||||||
import io.spring.application.Page;
|
import io.spring.application.Page;
|
||||||
import io.spring.application.data.ArticleDataList;
|
import io.spring.application.data.ArticleDataList;
|
||||||
import io.spring.application.ArticleQueryService;
|
import io.spring.core.article.ArticleRepository;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
import static io.spring.TestHelper.articleDataFixture;
|
import static io.spring.TestHelper.articleDataFixture;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@WebMvcTest(ArticlesApi.class)
|
||||||
@RunWith(SpringRunner.class)
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class ListArticleApiTest extends TestWithCurrentUser {
|
public class ListArticleApiTest extends TestWithCurrentUser {
|
||||||
|
@MockBean
|
||||||
|
private ArticleRepository articleRepository;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private ArticleQueryService articleQueryService;
|
private ArticleQueryService articleQueryService;
|
||||||
|
|
||||||
@LocalServerPort
|
@Autowired
|
||||||
private int port;
|
private MockMvc mvc;
|
||||||
|
|
||||||
|
@Override
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
super.setUp();
|
||||||
userFixture();
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -38,7 +45,7 @@ public class ListArticleApiTest extends TestWithCurrentUser {
|
|||||||
ArticleDataList articleDataList = new ArticleDataList(
|
ArticleDataList articleDataList = new ArticleDataList(
|
||||||
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
|
asList(articleDataFixture("1", user), articleDataFixture("2", user)), 2);
|
||||||
when(articleQueryService.findRecentArticles(eq(null), eq(null), eq(null), eq(new Page(0, 20)), eq(null))).thenReturn(articleDataList);
|
when(articleQueryService.findRecentArticles(eq(null), eq(null), eq(null), eq(new Page(0, 20)), eq(null))).thenReturn(articleDataList);
|
||||||
RestAssured.when()
|
RestAssuredMockMvc.when()
|
||||||
.get("/articles")
|
.get("/articles")
|
||||||
.prettyPeek()
|
.prettyPeek()
|
||||||
.then()
|
.then()
|
||||||
@ -47,7 +54,7 @@ public class ListArticleApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void should_get_feeds_401_without_login() throws Exception {
|
public void should_get_feeds_401_without_login() throws Exception {
|
||||||
RestAssured.when()
|
RestAssuredMockMvc.when()
|
||||||
.get("/articles/feed")
|
.get("/articles/feed")
|
||||||
.prettyPeek()
|
.prettyPeek()
|
||||||
.then()
|
.then()
|
||||||
|
@ -1,36 +1,38 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.application.data.ProfileData;
|
import io.spring.JacksonCustomizations;
|
||||||
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
import io.spring.application.ProfileQueryService;
|
import io.spring.application.ProfileQueryService;
|
||||||
|
import io.spring.application.data.ProfileData;
|
||||||
import io.spring.core.article.Article;
|
import io.spring.core.article.Article;
|
||||||
import io.spring.core.user.FollowRelation;
|
import io.spring.core.user.FollowRelation;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.mockito.Matchers.any;
|
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@WebMvcTest(ProfileApi.class)
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
|
||||||
public class ProfileApiTest extends TestWithCurrentUser {
|
public class ProfileApiTest extends TestWithCurrentUser {
|
||||||
@LocalServerPort
|
|
||||||
private int port;
|
|
||||||
private Article article;
|
private Article article;
|
||||||
private User anotherUser;
|
private User anotherUser;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private ProfileQueryService profileQueryService;
|
private ProfileQueryService profileQueryService;
|
||||||
|
|
||||||
@ -38,8 +40,8 @@ public class ProfileApiTest extends TestWithCurrentUser {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
super.setUp();
|
||||||
userFixture();
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
anotherUser = new User("username@test.com", "username", "123", "", "");
|
anotherUser = new User("username@test.com", "username", "123", "", "");
|
||||||
profileData = new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false);
|
profileData = new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false);
|
||||||
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
|
when(userRepository.findByUsername(eq(anotherUser.getUsername()))).thenReturn(Optional.of(anotherUser));
|
||||||
@ -49,7 +51,7 @@ public class ProfileApiTest extends TestWithCurrentUser {
|
|||||||
public void should_get_user_profile_success() throws Exception {
|
public void should_get_user_profile_success() throws Exception {
|
||||||
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(null)))
|
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(null)))
|
||||||
.thenReturn(Optional.of(profileData));
|
.thenReturn(Optional.of(profileData));
|
||||||
RestAssured.when()
|
RestAssuredMockMvc.when()
|
||||||
.get("/profiles/{username}", profileData.getUsername())
|
.get("/profiles/{username}", profileData.getUsername())
|
||||||
.prettyPeek()
|
.prettyPeek()
|
||||||
.then()
|
.then()
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.spring.core.service.JwtService;
|
|
||||||
import io.spring.application.data.UserData;
|
import io.spring.application.data.UserData;
|
||||||
import io.spring.infrastructure.mybatis.readservice.UserReadService;
|
import io.spring.core.service.JwtService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import io.spring.infrastructure.mybatis.readservice.UserReadService;
|
||||||
|
import org.junit.Before;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@ -13,7 +13,7 @@ import java.util.Optional;
|
|||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
class TestWithCurrentUser {
|
abstract class TestWithCurrentUser extends ApiTestBase {
|
||||||
@MockBean
|
@MockBean
|
||||||
protected UserRepository userRepository;
|
protected UserRepository userRepository;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ class TestWithCurrentUser {
|
|||||||
protected String username;
|
protected String username;
|
||||||
protected String defaultAvatar;
|
protected String defaultAvatar;
|
||||||
|
|
||||||
@Autowired
|
@MockBean
|
||||||
protected JwtService jwtService;
|
protected JwtService jwtService;
|
||||||
|
|
||||||
protected void userFixture() {
|
protected void userFixture() {
|
||||||
@ -42,6 +42,14 @@ class TestWithCurrentUser {
|
|||||||
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
|
||||||
when(userReadService.findById(eq(user.getId()))).thenReturn(userData);
|
when(userReadService.findById(eq(user.getId()))).thenReturn(userData);
|
||||||
|
|
||||||
token = jwtService.toToken(user);
|
token = "token";
|
||||||
|
when(jwtService.getSubFromToken(eq(token))).thenReturn(Optional.of(user.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
userFixture();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,35 +1,39 @@
|
|||||||
package io.spring.api;
|
package io.spring.api;
|
||||||
|
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||||
import io.spring.core.service.JwtService;
|
import io.spring.JacksonCustomizations;
|
||||||
|
import io.spring.api.security.WebSecurityConfig;
|
||||||
|
import io.spring.application.UserQueryService;
|
||||||
import io.spring.application.data.UserData;
|
import io.spring.application.data.UserData;
|
||||||
import io.spring.infrastructure.mybatis.readservice.UserReadService;
|
import io.spring.core.service.JwtService;
|
||||||
import io.spring.core.user.User;
|
import io.spring.core.user.User;
|
||||||
import io.spring.core.user.UserRepository;
|
import io.spring.core.user.UserRepository;
|
||||||
|
import io.spring.infrastructure.mybatis.readservice.UserReadService;
|
||||||
|
import io.spring.infrastructure.user.NaiveEncryptService;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static io.restassured.RestAssured.given;
|
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
|
||||||
import static org.hamcrest.core.IsEqual.equalTo;
|
import static org.hamcrest.core.IsEqual.equalTo;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
@WebMvcTest(UsersApi.class)
|
||||||
@RunWith(SpringRunner.class)
|
@Import({WebSecurityConfig.class, UserQueryService.class, NaiveEncryptService.class, JacksonCustomizations.class})
|
||||||
public class UsersApiTest {
|
public class UsersApiTest extends ApiTestBase {
|
||||||
@LocalServerPort
|
@Autowired
|
||||||
private int port;
|
private MockMvc mvc;
|
||||||
|
|
||||||
@MockBean
|
@MockBean
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
@ -44,7 +48,8 @@ public class UsersApiTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
RestAssured.port = port;
|
super.setUp();
|
||||||
|
RestAssuredMockMvc.mockMvc(mvc);
|
||||||
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user