Does Junit/Mockito hold value from actual method return?

34 Views Asked by At
@Test 
public void testTotalMarks(){
   Mark gMark = fakeMarkGenerator.createMarks();
   DBMark dbMark = markMapper.toDBMark(gMark);

   when(repoLayer.findMarksByStudentId()).thenReturn(Optional.of(dbMark)); 
 markService.createMarks(gMark); //Actually createMarks method return type is DBMark. 
//But this code works fine.
//Shouldn't this be DBMark dbMark2 =  markService.createMarks(gMark); ??

verify(mockMarkMapper, times(2)).toTotal(any(Total.class)); //I don't understand times here

Assert statement
}
1

There are 1 best solutions below

0
Omar Silva On

I don't understand times here

Times indicates the number of invocations you're expecting for the method you're verifying.

Shouldn't this be DBMark dbMark2 = markService.createMarks(gMark); ??

If you're trying to assert a value returning from a method you're testing, then yes, you should assign it to a variable and assert your value. Ex:

assertThat(dbMarks.size, <maybe your matcher here>)