use webmvctest

This commit is contained in:
aisensiy 2017-08-24 10:39:09 +08:00
parent 49030da9ca
commit 055dfcab13
10 changed files with 166 additions and 126 deletions

View File

@ -41,6 +41,8 @@ dependencies {
compileOnly('org.projectlombok:lombok')
runtime('com.h2database:h2')
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.restdocs:spring-restdocs-mockmvc')
testCompile('org.mybatis.spring.boot:mybatis-spring-boot-starter-test:1.3.0')

View 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 {
}
}

View File

@ -1,8 +1,10 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.data.ArticleData;
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.data.ArticleData;
import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
@ -12,25 +14,28 @@ import io.spring.core.favorite.ArticleFavoriteRepository;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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.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.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@WebMvcTest(ArticleFavoriteApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
public class ArticleFavoriteApiTest extends TestWithCurrentUser {
@Autowired
private MockMvc mvc;
@MockBean
private ArticleFavoriteRepository articleFavoriteRepository;
@ -40,22 +45,13 @@ public class ArticleFavoriteApiTest extends TestWithCurrentUser {
@MockBean
private ArticleQueryService articleQueryService;
protected String email;
protected String username;
protected String defaultAvatar;
@LocalServerPort
private int port;
private Article article;
private User anotherUser;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
email = "john@jacob.com";
username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
userFixture();
super.setUp();
RestAssuredMockMvc.mockMvc(mvc);
anotherUser = new User("other@test.com", "other", "123", "", "");
article = new Article("title", "desc", "body", new String[]{"java"}, anotherUser.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));

View File

@ -1,9 +1,11 @@
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.application.data.ArticleData;
import io.spring.api.security.WebSecurityConfig;
import io.spring.application.ArticleQueryService;
import io.spring.application.data.ArticleData;
import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.article.ArticleRepository;
@ -12,31 +14,30 @@ import org.joda.time.DateTime;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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.HashMap;
import java.util.Map;
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.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@WebMvcTest({ArticlesApi.class, ArticleApi.class})
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
public class ArticlesApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
@Autowired
private MockMvc mvc;
@MockBean
private ArticleRepository articleRepository;
@ -44,10 +45,11 @@ public class ArticlesApiTest extends TestWithCurrentUser {
@MockBean
private ArticleQueryService articleQueryService;
@Override
@Before
public void setUp() throws Exception {
RestAssured.port = port;
userFixture();
super.setUp();
RestAssuredMockMvc.mockMvc(mvc);
}
@Test
@ -122,7 +124,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
when(articleQueryService.findBySlug(eq(slug), eq(null))).thenReturn(Optional.of(articleData));
RestAssured.when()
RestAssuredMockMvc.when()
.get("/articles/{slug}", slug)
.then()
.statusCode(200)
@ -135,7 +137,7 @@ public class ArticlesApiTest extends TestWithCurrentUser {
@Test
public void should_404_if_article_not_found() throws Exception {
when(articleQueryService.findBySlug(anyString(), any())).thenReturn(Optional.empty());
RestAssured.when()
RestAssuredMockMvc.when()
.get("/articles/not-exists")
.then()
.statusCode(404);

View File

@ -1,6 +1,8 @@
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.data.CommentData;
import io.spring.application.data.ProfileData;
@ -11,33 +13,27 @@ import io.spring.core.comment.CommentRepository;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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.HashMap;
import java.util.Map;
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.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@SpringBootTest(webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
@WebMvcTest(CommentsApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
public class CommentsApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
protected String email;
protected String username;
protected String defaultAvatar;
@MockBean
private ArticleRepository articleRepository;
@ -50,15 +46,13 @@ public class CommentsApiTest extends TestWithCurrentUser {
private Article article;
private CommentData commentData;
private Comment comment;
@Autowired
private MockMvc mvc;
@Before
public void setUp() throws Exception {
RestAssured.port = port;
email = "john@jacob.com";
username = "johnjacob";
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
userFixture();
RestAssuredMockMvc.mockMvc(mvc);
super.setUp();
article = new Article("title", "desc", "body", new String[]{"test", "java"}, user.getId());
when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article));
comment = new Comment("comment", user.getId(), article.getId());
@ -115,7 +109,7 @@ public class CommentsApiTest extends TestWithCurrentUser {
@Test
public void should_get_comments_of_article_success() throws Exception {
when(commentQueryService.findByArticleId(anyString(), eq(null))).thenReturn(Arrays.asList(commentData));
RestAssured.when()
RestAssuredMockMvc.when()
.get("/articles/{slug}/comments", article.getSlug())
.prettyPeek()
.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 {
User anotherUser = new User("other@example.com", "other", "123", "", "");
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));
String token = jwtService.toToken(anotherUser);

View File

@ -1,41 +1,48 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.data.UserData;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import io.spring.JacksonCustomizations;
import io.spring.api.security.WebSecurityConfig;
import io.spring.application.UserQueryService;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import java.util.HashMap;
import java.util.Map;
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.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@SpringBootTest(webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
@WebMvcTest(CurrentUserApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
public class CurrentUserApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
@Autowired
private MockMvc mvc;
@MockBean
private UserQueryService userQueryService;
@Override
@Before
public void setUp() throws Exception {
RestAssured.port = port;
userFixture();
super.setUp();
RestAssuredMockMvc.mockMvc(mvc);
}
@Test
public void should_get_current_user_with_token() throws Exception {
when(userQueryService.findById(any())).thenReturn(Optional.of(userData));
given()
.header("Authorization", "Token " + token)
@ -64,9 +71,11 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
@Test
public void should_get_401_with_invalid_token() throws Exception {
String invalidToken = "asdfasd";
when(jwtService.getSubFromToken(eq(invalidToken))).thenReturn(Optional.empty());
given()
.contentType("application/json")
.header("Authorization", "Token asdfasd")
.header("Authorization", "Token " + invalidToken)
.when()
.get("/user")
.then()
@ -90,7 +99,7 @@ public class CurrentUserApiTest extends TestWithCurrentUser {
when(userRepository.findByUsername(eq(newUsername))).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()
.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.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()
.contentType("application/json")

View File

@ -1,36 +1,43 @@
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.data.ArticleDataList;
import io.spring.application.ArticleQueryService;
import io.spring.core.article.ArticleRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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 java.util.Arrays.asList;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
@WebMvcTest(ArticlesApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
public class ListArticleApiTest extends TestWithCurrentUser {
@MockBean
private ArticleRepository articleRepository;
@MockBean
private ArticleQueryService articleQueryService;
@LocalServerPort
private int port;
@Autowired
private MockMvc mvc;
@Override
@Before
public void setUp() throws Exception {
RestAssured.port = port;
userFixture();
super.setUp();
RestAssuredMockMvc.mockMvc(mvc);
}
@Test
@ -38,7 +45,7 @@ public class ListArticleApiTest extends TestWithCurrentUser {
ArticleDataList articleDataList = new ArticleDataList(
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);
RestAssured.when()
RestAssuredMockMvc.when()
.get("/articles")
.prettyPeek()
.then()
@ -47,7 +54,7 @@ public class ListArticleApiTest extends TestWithCurrentUser {
@Test
public void should_get_feeds_401_without_login() throws Exception {
RestAssured.when()
RestAssuredMockMvc.when()
.get("/articles/feed")
.prettyPeek()
.then()

View File

@ -1,36 +1,38 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.application.data.ProfileData;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import io.spring.JacksonCustomizations;
import io.spring.api.security.WebSecurityConfig;
import io.spring.application.ProfileQueryService;
import io.spring.application.data.ProfileData;
import io.spring.core.article.Article;
import io.spring.core.user.FollowRelation;
import io.spring.core.user.User;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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 static io.restassured.RestAssured.given;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.given;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@WebMvcTest(ProfileApi.class)
@Import({WebSecurityConfig.class, JacksonCustomizations.class})
public class ProfileApiTest extends TestWithCurrentUser {
@LocalServerPort
private int port;
private Article article;
private User anotherUser;
@Autowired
private MockMvc mvc;
@MockBean
private ProfileQueryService profileQueryService;
@ -38,8 +40,8 @@ public class ProfileApiTest extends TestWithCurrentUser {
@Before
public void setUp() throws Exception {
RestAssured.port = port;
userFixture();
super.setUp();
RestAssuredMockMvc.mockMvc(mvc);
anotherUser = new User("username@test.com", "username", "123", "", "");
profileData = new ProfileData(anotherUser.getId(), anotherUser.getUsername(), anotherUser.getBio(), anotherUser.getImage(), false);
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 {
when(profileQueryService.findByUsername(eq(profileData.getUsername()), eq(null)))
.thenReturn(Optional.of(profileData));
RestAssured.when()
RestAssuredMockMvc.when()
.get("/profiles/{username}", profileData.getUsername())
.prettyPeek()
.then()

View File

@ -1,11 +1,11 @@
package io.spring.api;
import io.spring.core.service.JwtService;
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.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 java.util.Optional;
@ -13,7 +13,7 @@ import java.util.Optional;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
class TestWithCurrentUser {
abstract class TestWithCurrentUser extends ApiTestBase {
@MockBean
protected UserRepository userRepository;
@ -27,7 +27,7 @@ class TestWithCurrentUser {
protected String username;
protected String defaultAvatar;
@Autowired
@MockBean
protected JwtService jwtService;
protected void userFixture() {
@ -42,6 +42,14 @@ class TestWithCurrentUser {
userData = new UserData(user.getId(), email, username, "", defaultAvatar);
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();
}
}

View File

@ -1,35 +1,39 @@
package io.spring.api;
import io.restassured.RestAssured;
import io.spring.core.service.JwtService;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import io.spring.JacksonCustomizations;
import io.spring.api.security.WebSecurityConfig;
import io.spring.application.UserQueryService;
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.UserRepository;
import io.spring.infrastructure.mybatis.readservice.UserReadService;
import io.spring.infrastructure.user.NaiveEncryptService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
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.Map;
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.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class UsersApiTest {
@LocalServerPort
private int port;
@WebMvcTest(UsersApi.class)
@Import({WebSecurityConfig.class, UserQueryService.class, NaiveEncryptService.class, JacksonCustomizations.class})
public class UsersApiTest extends ApiTestBase {
@Autowired
private MockMvc mvc;
@MockBean
private UserRepository userRepository;
@ -44,7 +48,8 @@ public class UsersApiTest {
@Before
public void setUp() throws Exception {
RestAssured.port = port;
super.setUp();
RestAssuredMockMvc.mockMvc(mvc);
defaultAvatar = "https://static.productionready.io/images/smiley-cyrus.jpg";
}