From 96008fcf5649ac83297b8043effdd071b04e44fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E4=B8=80=E4=B8=AA=E4=BA=BA=E5=9C=A8=E8=BF=99?= =?UTF-8?q?=E5=84=BF=E5=B9=B2=E5=98=9B=E4=BD=A0=E6=98=AF=E6=9D=A5=E6=8B=89?= =?UTF-8?q?=E5=B1=8E=E7=9A=84=E5=90=A7?= <1421374934@qq.com> Date: Thu, 23 Mar 2023 22:14:38 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E6=B2=A1=E6=9C=89=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ArticleThumbsUpService.java | 2 + .../forest/service/ArticleServiceTest.java | 9 +- .../service/ArticleThumbsUpServiceTest.java | 105 ++++++++++++++++++ 3 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/rymcu/forest/service/ArticleThumbsUpServiceTest.java 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