Migrated deprecated asserts to more explicit junit asserts

This commit is contained in:
James Menzies 2020-10-22 16:10:21 +01:00
parent 8dc8311832
commit 05091bba33
11 changed files with 127 additions and 124 deletions

View File

@ -11,8 +11,8 @@ import io.spring.core.favorite.ArticleFavoriteRepository;
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 io.spring.core.user.UserRepository; import io.spring.core.user.UserRepository;
import io.spring.infrastructure.repository.MyBatisArticleRepository;
import io.spring.infrastructure.repository.MyBatisArticleFavoriteRepository; import io.spring.infrastructure.repository.MyBatisArticleFavoriteRepository;
import io.spring.infrastructure.repository.MyBatisArticleRepository;
import io.spring.infrastructure.repository.MyBatisUserRepository; import io.spring.infrastructure.repository.MyBatisUserRepository;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
@ -25,9 +25,10 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MybatisTest @MybatisTest
@ -53,7 +54,7 @@ public class ArticleQueryServiceTest {
private Article article; private Article article;
@Before @Before
public void setUp() throws Exception { public void setUp() {
user = new User("aisensiy@gmail.com", "aisensiy", "123", "", ""); user = new User("aisensiy@gmail.com", "aisensiy", "123", "", "");
userRepository.save(user); userRepository.save(user);
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId(), new DateTime()); article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId(), new DateTime());
@ -61,46 +62,49 @@ public class ArticleQueryServiceTest {
} }
@Test @Test
public void should_fetch_article_success() throws Exception { public void should_fetch_article_success() {
Optional<ArticleData> optional = queryService.findById(article.getId(), user); Optional<ArticleData> optional = queryService.findById(article.getId(), user);
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
ArticleData fetched = optional.get(); ArticleData fetched = optional.get();
assertThat(fetched.getFavoritesCount(), is(0)); assertEquals(fetched.getFavoritesCount(),0);
assertThat(fetched.isFavorited(), is(false)); assertFalse(fetched.isFavorited());
assertThat(fetched.getCreatedAt(), notNullValue()); assertNotNull(fetched.getCreatedAt());
assertThat(fetched.getUpdatedAt(), notNullValue()); assertNotNull(fetched.getUpdatedAt());
assertThat(fetched.getTagList().contains("java"), is(true)); assertTrue(fetched.getTagList().contains("java"));
} }
@Test @Test
public void should_get_article_with_right_favorite_and_favorite_count() throws Exception { public void should_get_article_with_right_favorite_and_favorite_count() {
User anotherUser = new User("other@test.com", "other", "123", "", ""); User anotherUser = new User("other@test.com", "other", "123", "", "");
userRepository.save(anotherUser); userRepository.save(anotherUser);
articleFavoriteRepository.save(new ArticleFavorite(article.getId(), anotherUser.getId())); articleFavoriteRepository.save(new ArticleFavorite(article.getId(), anotherUser.getId()));
ArticleData articleData = queryService.findById(article.getId(), anotherUser).get(); Optional<ArticleData> optional = queryService.findById(article.getId(), anotherUser);
assertThat(articleData.getFavoritesCount(), is(1)); assertTrue(optional.isPresent());
assertThat(articleData.isFavorited(), is(true));
ArticleData articleData = optional.get();
assertEquals(articleData.getFavoritesCount(), 1);
assertTrue(articleData.isFavorited());
} }
@Test @Test
public void should_get_default_article_list() throws Exception { public void should_get_default_article_list() {
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId(), new DateTime().minusHours(1)); Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId(), new DateTime().minusHours(1));
articleRepository.save(anotherArticle); articleRepository.save(anotherArticle);
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), user); ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), user);
assertThat(recentArticles.getCount(), is(2)); assertEquals(recentArticles.getCount(), 2);
assertThat(recentArticles.getArticleDatas().size(), is(2)); assertEquals(recentArticles.getArticleDatas().size(), 2);
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId())); assertEquals(recentArticles.getArticleDatas().get(0).getId(), article.getId());
ArticleDataList nodata = queryService.findRecentArticles(null, null, null, new Page(2, 10), user); ArticleDataList nodata = queryService.findRecentArticles(null, null, null, new Page(2, 10), user);
assertThat(nodata.getCount(), is(2)); assertEquals(nodata.getCount(),2);
assertThat(nodata.getArticleDatas().size(), is(0)); assertEquals(nodata.getArticleDatas().size(), 0);
} }
@Test @Test
public void should_query_article_by_author() throws Exception { public void should_query_article_by_author() {
User anotherUser = new User("other@email.com", "other", "123", "", ""); User anotherUser = new User("other@email.com", "other", "123", "", "");
userRepository.save(anotherUser); userRepository.save(anotherUser);
@ -108,12 +112,12 @@ public class ArticleQueryServiceTest {
articleRepository.save(anotherArticle); articleRepository.save(anotherArticle);
ArticleDataList recentArticles = queryService.findRecentArticles(null, user.getUsername(), null, new Page(), user); ArticleDataList recentArticles = queryService.findRecentArticles(null, user.getUsername(), null, new Page(), user);
assertThat(recentArticles.getArticleDatas().size(), is(1)); assertEquals(recentArticles.getArticleDatas().size(), 1);
assertThat(recentArticles.getCount(), is(1)); assertEquals(recentArticles.getCount(), 1);
} }
@Test @Test
public void should_query_article_by_favorite() throws Exception { public void should_query_article_by_favorite() {
User anotherUser = new User("other@email.com", "other", "123", "", ""); User anotherUser = new User("other@email.com", "other", "123", "", "");
userRepository.save(anotherUser); userRepository.save(anotherUser);
@ -124,30 +128,30 @@ public class ArticleQueryServiceTest {
articleFavoriteRepository.save(articleFavorite); articleFavoriteRepository.save(articleFavorite);
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, anotherUser.getUsername(), new Page(), anotherUser); ArticleDataList recentArticles = queryService.findRecentArticles(null, null, anotherUser.getUsername(), new Page(), anotherUser);
assertThat(recentArticles.getArticleDatas().size(), is(1)); assertEquals(recentArticles.getArticleDatas().size(), 1);
assertThat(recentArticles.getCount(), is(1)); assertEquals(recentArticles.getCount(), 1);
ArticleData articleData = recentArticles.getArticleDatas().get(0); ArticleData articleData = recentArticles.getArticleDatas().get(0);
assertThat(articleData.getId(), is(article.getId())); assertEquals(articleData.getId(), article.getId());
assertThat(articleData.getFavoritesCount(), is(1)); assertEquals(articleData.getFavoritesCount(),1);
assertThat(articleData.isFavorited(), is(true)); assertTrue(articleData.isFavorited());
} }
@Test @Test
public void should_query_article_by_tag() throws Exception { public void should_query_article_by_tag() {
Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId()); Article anotherArticle = new Article("new article", "desc", "body", new String[]{"test"}, user.getId());
articleRepository.save(anotherArticle); articleRepository.save(anotherArticle);
ArticleDataList recentArticles = queryService.findRecentArticles("spring", null, null, new Page(), user); ArticleDataList recentArticles = queryService.findRecentArticles("spring", null, null, new Page(), user);
assertThat(recentArticles.getArticleDatas().size(), is(1)); assertEquals(recentArticles.getArticleDatas().size(), 1);
assertThat(recentArticles.getCount(), is(1)); assertEquals(recentArticles.getCount(), 1);
assertThat(recentArticles.getArticleDatas().get(0).getId(), is(article.getId())); assertEquals(recentArticles.getArticleDatas().get(0).getId(), article.getId());
ArticleDataList notag = queryService.findRecentArticles("notag", null, null, new Page(), user); ArticleDataList notag = queryService.findRecentArticles("notag", null, null, new Page(), user);
assertThat(notag.getCount(), is(0)); assertEquals(notag.getCount(), 0);
} }
@Test @Test
public void should_show_following_if_user_followed_author() throws Exception { public void should_show_following_if_user_followed_author() {
User anotherUser = new User("other@email.com", "other", "123", "", ""); User anotherUser = new User("other@email.com", "other", "123", "", "");
userRepository.save(anotherUser); userRepository.save(anotherUser);
@ -155,13 +159,13 @@ public class ArticleQueryServiceTest {
userRepository.saveRelation(followRelation); userRepository.saveRelation(followRelation);
ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), anotherUser); ArticleDataList recentArticles = queryService.findRecentArticles(null, null, null, new Page(), anotherUser);
assertThat(recentArticles.getCount(), is(1)); assertEquals(recentArticles.getCount(), 1);
ArticleData articleData = recentArticles.getArticleDatas().get(0); ArticleData articleData = recentArticles.getArticleDatas().get(0);
assertThat(articleData.getProfileData().isFollowing(), is(true)); assertTrue(articleData.getProfileData().isFollowing());
} }
@Test @Test
public void should_get_user_feed() throws Exception { public void should_get_user_feed() {
User anotherUser = new User("other@email.com", "other", "123", "", ""); User anotherUser = new User("other@email.com", "other", "123", "", "");
userRepository.save(anotherUser); userRepository.save(anotherUser);
@ -169,11 +173,11 @@ public class ArticleQueryServiceTest {
userRepository.saveRelation(followRelation); userRepository.saveRelation(followRelation);
ArticleDataList userFeed = queryService.findUserFeed(user, new Page()); ArticleDataList userFeed = queryService.findUserFeed(user, new Page());
assertThat(userFeed.getCount(), is(0)); assertEquals(userFeed.getCount(), 0);
ArticleDataList anotherUserFeed = queryService.findUserFeed(anotherUser, new Page()); ArticleDataList anotherUserFeed = queryService.findUserFeed(anotherUser, new Page());
assertThat(anotherUserFeed.getCount(), is(1)); assertEquals(anotherUserFeed.getCount(), 1);
ArticleData articleData = anotherUserFeed.getArticleDatas().get(0); ArticleData articleData = anotherUserFeed.getArticleDatas().get(0);
assertThat(articleData.getProfileData().isFollowing(), is(true)); assertTrue(articleData.getProfileData().isFollowing());
} }
} }

View File

@ -23,8 +23,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*; import static org.junit.Assert.assertTrue;
@MybatisTest @MybatisTest
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -45,24 +45,24 @@ public class CommentQueryServiceTest {
private User user; private User user;
@Before @Before
public void setUp() throws Exception { public void setUp() {
user = new User("aisensiy@test.com", "aisensiy", "123", "", ""); user = new User("aisensiy@test.com", "aisensiy", "123", "", "");
userRepository.save(user); userRepository.save(user);
} }
@Test @Test
public void should_read_comment_success() throws Exception { public void should_read_comment_success() {
Comment comment = new Comment("content", user.getId(), "123"); Comment comment = new Comment("content", user.getId(), "123");
commentRepository.save(comment); commentRepository.save(comment);
Optional<CommentData> optional = commentQueryService.findById(comment.getId(), user); Optional<CommentData> optional = commentQueryService.findById(comment.getId(), user);
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
CommentData commentData = optional.get(); CommentData commentData = optional.get();
assertThat(commentData.getProfileData().getUsername(), is(user.getUsername())); assertEquals(commentData.getProfileData().getUsername(), user.getUsername());
} }
@Test @Test
public void should_read_comments_of_article() throws Exception { public void should_read_comments_of_article() {
Article article = new Article("title", "desc", "body", new String[]{"java"}, user.getId()); Article article = new Article("title", "desc", "body", new String[]{"java"}, user.getId());
articleRepository.save(article); articleRepository.save(article);
@ -76,7 +76,7 @@ public class CommentQueryServiceTest {
commentRepository.save(comment2); commentRepository.save(comment2);
List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user); List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user);
assertThat(comments.size(), is(2)); assertEquals(comments.size(), 2);
} }
} }

View File

@ -14,8 +14,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MybatisTest @MybatisTest
@ -27,12 +26,12 @@ public class ProfileQueryServiceTest {
private UserRepository userRepository; private UserRepository userRepository;
@Test @Test
public void should_fetch_profile_success() throws Exception { public void should_fetch_profile_success() {
User currentUser = new User("a@test.com", "a", "123", "", ""); User currentUser = new User("a@test.com", "a", "123", "", "");
User profileUser = new User("p@test.com", "p", "123", "", ""); User profileUser = new User("p@test.com", "p", "123", "", "");
userRepository.save(profileUser); userRepository.save(profileUser);
Optional<ProfileData> optional = profileQueryService.findByUsername(profileUser.getUsername(), currentUser); Optional<ProfileData> optional = profileQueryService.findByUsername(profileUser.getUsername(), currentUser);
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
} }
} }

View File

@ -11,8 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MybatisTest @MybatisTest
@ -25,8 +24,8 @@ public class TagsQueryServiceTest {
private ArticleRepository articleRepository; private ArticleRepository articleRepository;
@Test @Test
public void should_get_all_tags() throws Exception { public void should_get_all_tags() {
articleRepository.save(new Article("test", "test", "test", new String[]{"java"}, "123")); articleRepository.save(new Article("test", "test", "test", new String[]{"java"}, "123"));
assertThat(tagsQueryService.allTags().contains("java"), is(true)); assertTrue(tagsQueryService.allTags().contains("java"));
} }
} }

View File

@ -8,31 +8,31 @@ import static org.hamcrest.MatcherAssert.assertThat;
public class ArticleTest { public class ArticleTest {
@Test @Test
public void should_get_right_slug() throws Exception { public void should_get_right_slug() {
Article article = new Article("a new title", "desc", "body", new String[]{"java"}, "123"); Article article = new Article("a new title", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("a-new-title")); assertThat(article.getSlug(), is("a-new-title"));
} }
@Test @Test
public void should_get_right_slug_with_number_in_title() throws Exception { public void should_get_right_slug_with_number_in_title() {
Article article = new Article("a new title 2", "desc", "body", new String[]{"java"}, "123"); Article article = new Article("a new title 2", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("a-new-title-2")); assertThat(article.getSlug(), is("a-new-title-2"));
} }
@Test @Test
public void should_get_lower_case_slug() throws Exception { public void should_get_lower_case_slug() {
Article article = new Article("A NEW TITLE", "desc", "body", new String[]{"java"}, "123"); Article article = new Article("A NEW TITLE", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("a-new-title")); assertThat(article.getSlug(), is("a-new-title"));
} }
@Test @Test
public void should_handle_other_language() throws Exception { public void should_handle_other_language() {
Article article = new Article("中文:标题", "desc", "body", new String[]{"java"}, "123"); Article article = new Article("中文:标题", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("中文-标题")); assertThat(article.getSlug(), is("中文-标题"));
} }
@Test @Test
public void should_handle_commas() throws Exception { public void should_handle_commas() {
Article article = new Article("what?the.hell,w", "desc", "body", new String[]{"java"}, "123"); Article article = new Article("what?the.hell,w", "desc", "body", new String[]{"java"}, "123");
assertThat(article.getSlug(), is("what-the-hell-w")); assertThat(article.getSlug(), is("what-the-hell-w"));
} }

View File

@ -12,8 +12,7 @@ import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabas
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@ -30,7 +29,7 @@ public class ArticleRepositoryTransactionTest {
@Test @Test
public void transactional_test() throws Exception { public void transactional_test() {
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default"); User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
userRepository.save(user); userRepository.save(user);
Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId()); Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
@ -39,7 +38,7 @@ public class ArticleRepositoryTransactionTest {
try { try {
articleRepository.save(anotherArticle); articleRepository.save(anotherArticle);
} catch (Exception e) { } catch (Exception e) {
assertThat(articleMapper.findTag("other"), is(false)); assertFalse(articleMapper.findTag("other"));
} }
} }

View File

@ -17,9 +17,10 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
@MybatisTest @MybatisTest
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -35,24 +36,24 @@ public class MyBatisArticleRepositoryTest {
@Before @Before
public void setUp() throws Exception { public void setUp() {
User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default"); User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default");
userRepository.save(user); userRepository.save(user);
article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId()); article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId());
} }
@Test @Test
public void should_create_and_fetch_article_success() throws Exception { public void should_create_and_fetch_article_success() {
articleRepository.save(article); articleRepository.save(article);
Optional<Article> optional = articleRepository.findById(article.getId()); Optional<Article> optional = articleRepository.findById(article.getId());
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
assertThat(optional.get(), is(article)); assertEquals(optional.get(), article);
assertThat(optional.get().getTags().contains(new Tag("java")), is(true)); assertTrue(optional.get().getTags().contains(new Tag("java")));
assertThat(optional.get().getTags().contains(new Tag("spring")), is(true)); assertTrue(optional.get().getTags().contains(new Tag("spring")));
} }
@Test @Test
public void should_update_and_fetch_article_success() throws Exception { public void should_update_and_fetch_article_success() {
articleRepository.save(article); articleRepository.save(article);
String newTitle = "new test 2"; String newTitle = "new test 2";
@ -60,17 +61,17 @@ public class MyBatisArticleRepositoryTest {
articleRepository.save(article); articleRepository.save(article);
System.out.println(article.getSlug()); System.out.println(article.getSlug());
Optional<Article> optional = articleRepository.findBySlug(article.getSlug()); Optional<Article> optional = articleRepository.findBySlug(article.getSlug());
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
Article fetched = optional.get(); Article fetched = optional.get();
assertThat(fetched.getTitle(), is(newTitle)); assertEquals(fetched.getTitle(), newTitle);
assertThat(fetched.getBody(), not("")); assertNotEquals(fetched.getBody(), "");
} }
@Test @Test
public void should_delete_article() throws Exception { public void should_delete_article() {
articleRepository.save(article); articleRepository.save(article);
articleRepository.remove(article); articleRepository.remove(article);
assertThat(articleRepository.findById(article.getId()).isPresent(), is(false)); assertFalse(articleRepository.findById(article.getId()).isPresent());
} }
} }

View File

@ -12,8 +12,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*; import static org.junit.Assert.assertTrue;
@MybatisTest @MybatisTest
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ -23,12 +23,12 @@ public class MyBatisCommentRepositoryTest {
private CommentRepository commentRepository; private CommentRepository commentRepository;
@Test @Test
public void should_create_and_fetch_comment_success() throws Exception { public void should_create_and_fetch_comment_success() {
Comment comment = new Comment("content", "123", "456"); Comment comment = new Comment("content", "123", "456");
commentRepository.save(comment); commentRepository.save(comment);
Optional<Comment> optional = commentRepository.findById("456", comment.getId()); Optional<Comment> optional = commentRepository.findById("456", comment.getId());
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
assertThat(optional.get(), is(comment)); assertEquals(optional.get(), comment);
} }
} }

View File

@ -10,9 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertFalse;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MybatisTest @MybatisTest
@ -25,17 +24,17 @@ public class MyBatisArticleFavoriteRepositoryTest {
private io.spring.infrastructure.mybatis.mapper.ArticleFavoriteMapper articleFavoriteMapper; private io.spring.infrastructure.mybatis.mapper.ArticleFavoriteMapper articleFavoriteMapper;
@Test @Test
public void should_save_and_fetch_articleFavorite_success() throws Exception { public void should_save_and_fetch_articleFavorite_success() {
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456"); ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
articleFavoriteRepository.save(articleFavorite); articleFavoriteRepository.save(articleFavorite);
assertThat(articleFavoriteMapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()), notNullValue()); assertNotNull(articleFavoriteMapper.find(articleFavorite.getArticleId(), articleFavorite.getUserId()));
} }
@Test @Test
public void should_remove_favorite_success() throws Exception { public void should_remove_favorite_success() {
ArticleFavorite articleFavorite = new ArticleFavorite("123", "456"); ArticleFavorite articleFavorite = new ArticleFavorite("123", "456");
articleFavoriteRepository.save(articleFavorite); articleFavoriteRepository.save(articleFavorite);
articleFavoriteRepository.remove(articleFavorite); articleFavoriteRepository.remove(articleFavorite);
assertThat(articleFavoriteRepository.find("123", "456").isPresent(), is(false)); assertFalse(articleFavoriteRepository.find("123", "456").isPresent());
} }
} }

View File

@ -7,38 +7,39 @@ import org.junit.Test;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals;
import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class DefaultJwtServiceTest { public class DefaultJwtServiceTest {
private JwtService jwtService; private JwtService jwtService;
@Before @Before
public void setUp() throws Exception { public void setUp() {
jwtService = new DefaultJwtService("123123", 3600); jwtService = new DefaultJwtService("123123", 3600);
} }
@Test @Test
public void should_generate_and_parse_token() throws Exception { public void should_generate_and_parse_token() {
User user = new User("email@email.com", "username", "123", "", ""); User user = new User("email@email.com", "username", "123", "", "");
String token = jwtService.toToken(user); String token = jwtService.toToken(user);
assertThat(token, notNullValue()); assertNotNull(token);
Optional<String> optional = jwtService.getSubFromToken(token); Optional<String> optional = jwtService.getSubFromToken(token);
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
assertThat(optional.get(), is(user.getId())); assertEquals(optional.get(), user.getId());
} }
@Test @Test
public void should_get_null_with_wrong_jwt() throws Exception { public void should_get_null_with_wrong_jwt() {
Optional<String> optional = jwtService.getSubFromToken("123"); Optional<String> optional = jwtService.getSubFromToken("123");
assertThat(optional.isPresent(), is(false)); assertFalse(optional.isPresent());
} }
@Test @Test
public void should_get_null_with_expired_jwt() throws Exception { public void should_get_null_with_expired_jwt() {
String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhaXNlbnNpeSIsImV4cCI6MTUwMjE2MTIwNH0.SJB-U60WzxLYNomqLo4G3v3LzFxJKuVrIud8D8Lz3-mgpo9pN1i7C8ikU_jQPJGm8HsC1CquGMI-rSuM7j6LDA"; String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhaXNlbnNpeSIsImV4cCI6MTUwMjE2MTIwNH0.SJB-U60WzxLYNomqLo4G3v3LzFxJKuVrIud8D8Lz3-mgpo9pN1i7C8ikU_jQPJGm8HsC1CquGMI-rSuM7j6LDA";
assertThat(jwtService.getSubFromToken(token).isPresent(), is(false)); assertFalse(jwtService.getSubFromToken(token).isPresent());
} }
} }

View File

@ -14,8 +14,9 @@ import org.springframework.test.context.junit4.SpringRunner;
import java.util.Optional; import java.util.Optional;
import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@MybatisTest @MybatisTest
@ -26,49 +27,49 @@ public class MyBatisUserRepositoryTest {
private User user; private User user;
@Before @Before
public void setUp() throws Exception { public void setUp() {
user = new User("aisensiy@163.com", "aisensiy", "123", "", "default"); user = new User("aisensiy@163.com", "aisensiy", "123", "", "default");
} }
@Test @Test
public void should_save_and_fetch_user_success() throws Exception { public void should_save_and_fetch_user_success() {
userRepository.save(user); userRepository.save(user);
Optional<User> userOptional = userRepository.findByUsername("aisensiy"); Optional<User> userOptional = userRepository.findByUsername("aisensiy");
assertThat(userOptional.get(), is(user)); assertEquals(userOptional.get(), user);
Optional<User> userOptional2 = userRepository.findByEmail("aisensiy@163.com"); Optional<User> userOptional2 = userRepository.findByEmail("aisensiy@163.com");
assertThat(userOptional2.get(), is(user)); assertEquals(userOptional2.get(), user);
} }
@Test @Test
public void should_update_user_success() throws Exception { public void should_update_user_success() {
String newEmail = "newemail@email.com"; String newEmail = "newemail@email.com";
user.update(newEmail, "", "", "", ""); user.update(newEmail, "", "", "", "");
userRepository.save(user); userRepository.save(user);
Optional<User> optional = userRepository.findByUsername(user.getUsername()); Optional<User> optional = userRepository.findByUsername(user.getUsername());
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
assertThat(optional.get().getEmail(), is(newEmail)); assertEquals(optional.get().getEmail(), newEmail);
String newUsername = "newUsername"; String newUsername = "newUsername";
user.update("", newUsername, "", "", ""); user.update("", newUsername, "", "", "");
userRepository.save(user); userRepository.save(user);
optional = userRepository.findByEmail(user.getEmail()); optional = userRepository.findByEmail(user.getEmail());
assertThat(optional.isPresent(), is(true)); assertTrue(optional.isPresent());
assertThat(optional.get().getUsername(), is(newUsername)); assertEquals(optional.get().getUsername(), newUsername);
assertThat(optional.get().getImage(), is(user.getImage())); assertEquals(optional.get().getImage(), user.getImage());
} }
@Test @Test
public void should_create_new_user_follow_success() throws Exception { public void should_create_new_user_follow_success() {
User other = new User("other@example.com", "other", "123", "", ""); User other = new User("other@example.com", "other", "123", "", "");
userRepository.save(other); userRepository.save(other);
FollowRelation followRelation = new FollowRelation(user.getId(), other.getId()); FollowRelation followRelation = new FollowRelation(user.getId(), other.getId());
userRepository.saveRelation(followRelation); userRepository.saveRelation(followRelation);
assertThat(userRepository.findRelation(user.getId(), other.getId()).isPresent(), is(true)); assertTrue(userRepository.findRelation(user.getId(), other.getId()).isPresent());
} }
@Test @Test
public void should_unfollow_user_success() throws Exception { public void should_unfollow_user_success() {
User other = new User("other@example.com", "other", "123", "", ""); User other = new User("other@example.com", "other", "123", "", "");
userRepository.save(other); userRepository.save(other);
@ -76,6 +77,6 @@ public class MyBatisUserRepositoryTest {
userRepository.saveRelation(followRelation); userRepository.saveRelation(followRelation);
userRepository.removeRelation(followRelation); userRepository.removeRelation(followRelation);
assertThat(userRepository.findRelation(user.getId(), other.getId()).isPresent(), is(false)); assertFalse(userRepository.findRelation(user.getId(), other.getId()).isPresent());
} }
} }