-1

I'm unit testing a Spring boot web app with Mockito. One of my methods is returning a Actorrest, but if I try to test it, I get compilation errors.

This is the test I wrote:

@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());

}

And this is the method I'm trying to test:

@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);

}

and the database mockdata are

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;
        
    }}

log error


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)

7
  • Whats the compilation error. Can you paste that log as well Commented Sep 9, 2021 at 20:05
  • ok will update @Snigdhajyoti Commented Sep 9, 2021 at 20:10
  • Does this answer your question? JUnit testing got initializationError with java.lang.Exception: No tests found matching Commented Sep 9, 2021 at 20:12
  • no, my issue is different Commented Sep 9, 2021 at 20:14
  • If you are using JUnit 4 try making the test method public. Commented Sep 9, 2021 at 20:33

1 Answer 1

0

With the JUnit 4 test runner you are using, the test methods must be defined as public.

Sign up to request clarification or add additional context in comments.

1 Comment

same problem sir!!1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.