diff --git a/src/main/java/com/rymcu/forest/service/ArticleThumbsUpService.java b/src/main/java/com/rymcu/forest/service/ArticleThumbsUpService.java index 6185201..0edca81 100644 --- a/src/main/java/com/rymcu/forest/service/ArticleThumbsUpService.java +++ b/src/main/java/com/rymcu/forest/service/ArticleThumbsUpService.java @@ -4,6 +4,8 @@ import com.rymcu.forest.core.service.Service; import com.rymcu.forest.entity.ArticleThumbsUp; /** + * 点赞 + * * @author ronger */ public interface ArticleThumbsUpService extends Service { diff --git a/src/test/java/com/rymcu/forest/service/ArticleServiceTest.java b/src/test/java/com/rymcu/forest/service/ArticleServiceTest.java index 6d272ef..817763e 100644 --- a/src/test/java/com/rymcu/forest/service/ArticleServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/ArticleServiceTest.java @@ -1,5 +1,6 @@ package com.rymcu.forest.service; +import cn.hutool.core.collection.CollectionUtil; import com.rymcu.forest.dto.ArticleDTO; import com.rymcu.forest.dto.ArticleSearchDTO; import com.rymcu.forest.dto.ArticleTagDTO; @@ -18,7 +19,6 @@ import org.springframework.transaction.annotation.Transactional; import java.io.UnsupportedEncodingException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -98,8 +98,8 @@ class ArticleServiceTest { @BeforeEach public void postArticle() throws UnsupportedEncodingException { Long articleId = articleService.postArticle(testArticle, testUser); - testArticle.setIdArticle(articleId); assertNotNull(articleId); + testArticle.setIdArticle(articleId); } /** @@ -111,8 +111,7 @@ class ArticleServiceTest { void findArticles() { // 无参数时返回参数不应为EmptyList List articlesAll = articleService.findArticles(new ArticleSearchDTO()); - assertNotNull(articlesAll); - assertNotEquals(Collections.emptyList(), articlesAll); + assertTrue(CollectionUtil.isNotEmpty(articlesAll)); // 测试条件查询是否含有目标数据 ArticleSearchDTO articleSearchDTO = new ArticleSearchDTO(); @@ -151,7 +150,7 @@ class ArticleServiceTest { @Test void findUserArticlesByIdUser() { List userArticlesByIdUser = articleService.findUserArticlesByIdUser(testArticle.getArticleAuthorId()); - assertNotEquals(Collections.emptyList(), userArticlesByIdUser); + assertTrue(CollectionUtil.isNotEmpty(userArticlesByIdUser)); } /** diff --git a/src/test/java/com/rymcu/forest/service/ArticleThumbsUpServiceTest.java b/src/test/java/com/rymcu/forest/service/ArticleThumbsUpServiceTest.java new file mode 100644 index 0000000..2feb248 --- /dev/null +++ b/src/test/java/com/rymcu/forest/service/ArticleThumbsUpServiceTest.java @@ -0,0 +1,105 @@ +package com.rymcu.forest.service; + +import com.rymcu.forest.core.exception.BusinessException; +import com.rymcu.forest.core.exception.ServiceException; +import com.rymcu.forest.dto.ArticleDTO; +import com.rymcu.forest.dto.ArticleSearchDTO; +import com.rymcu.forest.entity.ArticleThumbsUp; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Date; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * 点赞文章 + * + * @author 毛毛虫 + */ +@SpringBootTest +@ExtendWith(SpringExtension.class) +@Transactional +// 顺序执行单元测试 +@TestMethodOrder(MethodOrderer.Random.class) +class ArticleThumbsUpServiceTest { + + /** + * 测试用点赞实体 + */ + private final ArticleThumbsUp articleThumbsUp; + + @Autowired + private ArticleThumbsUpService articleThumbsUpService; + @Autowired + private ArticleService articleService; + + { + { + articleThumbsUp = new ArticleThumbsUp(); + articleThumbsUp.setIdArticle(-1L); + articleThumbsUp.setThumbsUpTime(new Date()); + articleThumbsUp.setIdUser(-1L); + + } + } + + + /** + * 测试点赞不存在的文章 + */ + @Test + void thumbsNotExistsArticle() { + assertThrows(BusinessException.class, () -> { + articleThumbsUpService.thumbsUp(articleThumbsUp); + }); + } + + /** + * 测试点赞存在的文章 + */ + @Test + void thumbsExistsArticle() { + assertThrows(NullPointerException.class, () -> { + assertDoesNotThrow(() -> { + List articles = articleService.findArticles(null); + }); + + }); + } + + /** + * 测试点赞存在的文章 + */ + @Test + void thumbsExistsArticle2() { + assertDoesNotThrow(() -> { + List articles = articleService.findArticles(new ArticleSearchDTO()); + articleThumbsUp.setIdArticle(articles.get(0).getIdArticle()); + }); + + int i = articleThumbsUpService.thumbsUp(articleThumbsUp); + assertEquals(1, i); + } + + + /** + * 测试点赞存在的文章 + */ + @Test + void thumbsNotExistsUser() { + articleThumbsUp.setIdUser(null); + assertThrows(ServiceException.class, () -> { + articleThumbsUpService.thumbsUp(articleThumbsUp); + }); + } + + +} \ No newline at end of file