【发布时间】:2021-11-06 11:11:13
【问题描述】:
我正在使用 Mockito 对 Spring Boot Web 应用程序进行单元测试。我的一种方法是返回一个 Actorrest,但如果我尝试对其进行测试,则会出现编译错误。
这是我写的测试:
@Test
void AddActorToChapters() throws NetflixException{
when(actorRepository.findById(MockData.getActor().getId())).thenReturn(Optional.of(MockData.getActor()));
when(chapterService.findChapterById(MockData.getChapter().getId())).thenReturn((MockData.getChapter()));
service.AddActorToChapter(MockData.getChapter().getId(), MockData.getActor().getId());
assertEquals(MockData.getActor().getChapters(), MockData.getChapter());
}
这是我正在尝试测试的方法:
@Override
public ActorParticipant AddActorToChapter(Long idActor, Long idChapter) throws NetflixException {
Actor actor = actorRepository.findById(idActor)
.orElseThrow(() -> new NotFoundException("Actor id not found - " + idActor));
if (actorRepository.existsByIdAndChapters_Id(idActor, idChapter)) {
throw new DuplicateException("Actor id found - " + idActor);
}
Chapter chapter = chapterService.findChapterById(idChapter);
actor.getChapters().add(chapter);
actorRepository.save(actor);
ActorParticpateMapper mapper = new ActorParticpateMapper();
return mapper.mapActorToActorParticipant(actor);
}
和数据库模拟数据是
public class MockData {
public static Actor getActor() {
Actor actor = new Actor();
actor.setId(1L);
actor.setName("ali");
actor.setNationality("ameriacn");
actor.getChapters().add(getChapter());
return actor;
}
public static Chapter getChapter() {
Chapter chapter = new Chapter();
chapter.setId(1L);
chapter.setName("Chapter 7");
return chapter;
}}
日志错误
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=AddActorToChapters], {ExactMatcher:fDisplayName=AddActorToChapters(actortest.ActorServiceTesting)], {LeadingIdentifierMatcher:fClassName=actortest.ActorServiceTesting,fLeadingIdentifier=AddActorToChapters]] from org.junit.internal.requests.ClassRequest@64b8f8f4
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:74)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:513)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
【问题讨论】:
-
什么是编译错误。你能把那个日志也贴一下吗
-
好的将更新@Snigdhajyoti
-
不,我的问题不同
-
如果您使用的是 JUnit 4,请尝试公开测试方法。
标签: spring-boot junit mockito