0
@ExtendWith(MockitoExtension.class)
class taskTestMain {

    @InjectMocks
    task task1;


    @Mock
    private statusRepository statusRepo;

    
    
    @Mock
     status statusRec;
    @Test
    void test() {
        Mockito.when(statusRepo.save(statusRec)).thenReturn(statusRec);

        task1.method();
    }
}

class task
{
method()
{
statusRec = statusRepo.save(statusRec); //after this step I get Potential stubbing exception 

}}

I have tried several ways . I am not able to figure this out. Please suggest how to proceed. Below I have pasted the junit error. task- Strict stubbing argument mismatch. Please check:

  • this invocation of 'save' method:statusRepo.save(status@8bffb8b); -> at task.run(task.java:80)
  • has following stubbing(s) with different arguments: 1. statusRepo.save(status@26a2f7f9 ); -> at .taskTestMain.test(taskTestMain.java:110) Typically, stubbing argument mismatch indicates user mistake when writing tests. Mockito fails early so that you can debug potential problem easily.

Thanks for your time

Namagiri Sridhar
  • 177
  • 1
  • 6
  • 18
  • 1
    The error is clear enough, you have different argument. When you mock save method, you jave to be sure that you are passing same object. Therefore your equals and hashcode method is not good, check that first – Lemmy Jan 22 '21 at 07:46

1 Answers1

3

To be honest I'am not totally sure about this but I have a guess.

Mockito.when(statusRepo.save(statusRec)).thenReturn(statusRec);

With this stub you simply want return the first parameter passed into the save Method? If this is true there is a more generic way to do this.

when(statusRepo.save(any(TcMdhTotemConsensusStatus.class))).then(returnsFirstArg());

This stub will always return the first argument that is passed into your save method.
See this stackoverflow post for more information. There also some alternatives in case your Java or Mockito Version does not match.

Jan Schmitz
  • 1,181
  • 9
  • 13