【问题标题】:How do you assert with add object to another object many to many你如何断言将对象添加到另一个对象多对多
【发布时间】: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)

【问题讨论】:

标签: spring-boot junit mockito


【解决方案1】:

对于您使用的 JUnit 4 测试运行程序,测试方法必须定义为 public。

【讨论】:

  • 同样的问题先生!!1
猜你喜欢
  • 2021-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2017-02-14
  • 2018-10-05
  • 1970-01-01
相关资源
最近更新 更多