【发布时间】:2022-01-04 18:43:12
【问题描述】:
说明
我在 mobgoDB 数据库中测试批量插入,在 @987654324@ 方法中我实例化数据库,在@AfterEach 我删除在 puzzles-today 集合中创建的所有文档。
但最后一个总是失败,有随机结果,有时测试通过有时显示这个367 or 366 or 364 or 364.
我添加了@RepeatedTest(4) 以确保它不会发生变化。
代码:
类测试
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import com.github.mathieusoysal.codingame_stats.CodinGame;
import com.github.mathieusoysal.codingame_stats.puzzle.Puzzle;
import org.bson.Document;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import io.github.mathieusoysal.util.MongoDBMockTest;
public class PuzzleDaoTest extends MongoDBMockTest {
private PuzzlesDao puzzleDao;
@BeforeEach
public void setUp() throws Exception {
super.setUp();
puzzleDao = new PuzzlesDao(mongoClient, "CodinGame-stats");
}
@AfterEach
public void tearDown() throws Exception {
mongoClient.getDatabase("CodinGame-stats")
.getCollection(PuzzlesDao.PUZZLES_HISTORY_COLLECTION)
.deleteMany(new Document());
super.tearDown();
}
@Test
void testSaveAll_shouldReturnTrue_withTwoPuzzles() {
List<Puzzle> puzzles = new CodinGame().getPuzzles().subList(0, 2);
assertTrue(puzzleDao.saveAll(puzzles));
}
@Test
void testSaveAll_shouldAugmentCollectionSize_withTwoPuzzles() {
List<Puzzle> puzzles = new CodinGame().getPuzzles().subList(0, 2);
puzzleDao.saveAll(puzzles);
assertEquals(puzzles.size(), countDocuments());
}
@RepeatedTest(4)
void testSaveAll_shouldAugmentCollectionSize_withAllPuzzles() {
List<Puzzle> puzzles = new CodinGame().getPuzzles();
puzzleDao.saveAll(puzzles);
assertEquals(puzzles.size(), countDocuments());
}
private long countDocuments() {
return mongoClient.getDatabase("CodinGame-stats")
.getCollection(PuzzlesDao.PUZZLES_HISTORY_COLLECTION)
.countDocuments();
}
}
克隆项目
要轻松复制此内容,您可以克隆 dev 分支 https://github.com/MathieuSoysal/CodinGame-Puzzles-stats-saver/tree/dev 中的存储库
错误显示:
但是这个结果是随机的,有时测试通过有时会显示错误。
有人有办法解决这个问题吗?
【问题讨论】:
-
这里发生了太多未知的事情。我建议您创建一个包含所有详细信息的最小示例来复制您的问题。否则外界很难搞清楚。
-
@johanneslink 感谢您的反馈,我添加了更多细节来重现问题。
-
失败信息是什么?
-
我编辑了我的问题以添加失败消息。
-
如果您想提高有人会帮助您解决问题的机会,请尽可能轻松地为他们复制问题。必须将我自己的 pom.xml 放在一起并将几个源复制到一个我必须自己配置的项目中,这远远超出了我的门槛。其他人的里程可能会有所不同。
标签: java mongodb unit-testing testing junit5