🐛 没有测试的单元测试

This commit is contained in:
你一个人在这儿干嘛你是来拉屎的吧 2023-03-23 22:14:38 +08:00
parent 8be50b349a
commit 96008fcf56
3 changed files with 111 additions and 5 deletions

View File

@ -4,6 +4,8 @@ import com.rymcu.forest.core.service.Service;
import com.rymcu.forest.entity.ArticleThumbsUp; import com.rymcu.forest.entity.ArticleThumbsUp;
/** /**
* 点赞
*
* @author ronger * @author ronger
*/ */
public interface ArticleThumbsUpService extends Service<ArticleThumbsUp> { public interface ArticleThumbsUpService extends Service<ArticleThumbsUp> {

View File

@ -1,5 +1,6 @@
package com.rymcu.forest.service; package com.rymcu.forest.service;
import cn.hutool.core.collection.CollectionUtil;
import com.rymcu.forest.dto.ArticleDTO; import com.rymcu.forest.dto.ArticleDTO;
import com.rymcu.forest.dto.ArticleSearchDTO; import com.rymcu.forest.dto.ArticleSearchDTO;
import com.rymcu.forest.dto.ArticleTagDTO; import com.rymcu.forest.dto.ArticleTagDTO;
@ -18,7 +19,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -98,8 +98,8 @@ class ArticleServiceTest {
@BeforeEach @BeforeEach
public void postArticle() throws UnsupportedEncodingException { public void postArticle() throws UnsupportedEncodingException {
Long articleId = articleService.postArticle(testArticle, testUser); Long articleId = articleService.postArticle(testArticle, testUser);
testArticle.setIdArticle(articleId);
assertNotNull(articleId); assertNotNull(articleId);
testArticle.setIdArticle(articleId);
} }
/** /**
@ -111,8 +111,7 @@ class ArticleServiceTest {
void findArticles() { void findArticles() {
// 无参数时返回参数不应为EmptyList // 无参数时返回参数不应为EmptyList
List<ArticleDTO> articlesAll = articleService.findArticles(new ArticleSearchDTO()); List<ArticleDTO> articlesAll = articleService.findArticles(new ArticleSearchDTO());
assertNotNull(articlesAll); assertTrue(CollectionUtil.isNotEmpty(articlesAll));
assertNotEquals(Collections.emptyList(), articlesAll);
// 测试条件查询是否含有目标数据 // 测试条件查询是否含有目标数据
ArticleSearchDTO articleSearchDTO = new ArticleSearchDTO(); ArticleSearchDTO articleSearchDTO = new ArticleSearchDTO();
@ -151,7 +150,7 @@ class ArticleServiceTest {
@Test @Test
void findUserArticlesByIdUser() { void findUserArticlesByIdUser() {
List<ArticleDTO> userArticlesByIdUser = articleService.findUserArticlesByIdUser(testArticle.getArticleAuthorId()); List<ArticleDTO> userArticlesByIdUser = articleService.findUserArticlesByIdUser(testArticle.getArticleAuthorId());
assertNotEquals(Collections.emptyList(), userArticlesByIdUser); assertTrue(CollectionUtil.isNotEmpty(userArticlesByIdUser));
} }
/** /**

View File

@ -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<ArticleDTO> articles = articleService.findArticles(null);
});
});
}
/**
* 测试点赞存在的文章
*/
@Test
void thumbsExistsArticle2() {
assertDoesNotThrow(() -> {
List<ArticleDTO> 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);
});
}
}