Reputation: 450
I am making unit tests in my spring boot project and I am using mockito to mock instances. CalculeMajStockServiceImpl is the class I want to test and getEtatTranche is the method I want to test.
This is my test class.The problem here is that the real getDateCouplageParCycleEtParTrancheEtParLibelle method is called.
@RunWith(MockitoJUnitRunner.class)
class CalculeMajStockServiceImplTest {
@Mock
private ServiceBddProxy serviceBddProxy;
@Mock
private ServiceDataExcelProxy serviceDataExcelProxy;
@Mock
private ServiceAccessDonneesProxy serviceAccessDonneesProxy;
@Mock
private ParseFichierC3poServiceImpl parseFichierC3poServiceImpl;
@Mock
private ParseFichierRealiseRteServiceImpl parseFichierRealiseRteServiceImpl;
@Mock
private ParseFichierHebdoServiceImpl parseFichierHebdoServiceImpl;
@Spy
CalculeMajStockServiceImpl calculeMajStockServiceImpl = Mockito.spy(CalculeMajStockServiceImpl.class);
@Before
void setMockOutput() {
MockitoAnnotations.initMocks(this);
}
@Test
public void getEtatTranche() {
Date dateExtrapolation = new Date(2020, 05, 06);
Date dateDeCouplage = new Date(2020, 04, 06);
Date dateCourante = new Date(2020, 03, 20);
Date dateCoouplageLibellePrecedent = new Date(2020, 03, 19);
Long idLibellePrecedent = 1L;
String trancheCode = "BELV1";
int numCycle = 23;
LibelleDto libelle = new LibelleDto();
libelle.setDateCourante(dateCourante);
LibelleDto libellePrecedent = new LibelleDto();
libellePrecedent.setDateCourante(dateCourante);
// CAS OU dateCourante>=dateCouplageLibellePrecedent et
// dateExtrapolation>dateCouplage
doReturn(dateCoouplageLibellePrecedent).when(calculeMajStockServiceImpl)
.getDateCouplageParCycleEtParTrancheEtParLibelle(Mockito.anyLong(), Mockito.anyString(),
Mockito.anyInt());
Integer calculated = this.calculeMajStockServiceImpl.getEtatTranche(dateDeCouplage,
trancheCode, numCycle, libellePrecedent, new Date());
verify(calculeMajStockServiceImpl).getDateCouplageParCycleEtParTrancheEtParLibelle(Mockito.anyLong(), Mockito.anyString(), Mockito.anyInt());
// verify(calculeMajStockServiceImpl).findLibelleById(Mockito.anyLong());
Integer expected = 2;
assertEquals(expected, calculated);}
}
and this is the method i want to test
@Override
public int getEtatTranche(Date dateCouplage, String trancheCode, int numCycle,
LibelleDto libellePrecedent, Date dateCourante) {
int etatTranche = 0;
if (dateCourante != null && dateCouplage != null) {
if (dateCouplage.compareTo(dateCourante) > 0) {
Date dateCouplageLibellePrecedent = this.getDateCouplageParCycleEtParTrancheEtParLibelle(
libellePrecedent.getId(), trancheCode, numCycle);
if (dateCouplageLibellePrecedent != null && dateCourante != null
&& dateCouplageLibellePrecedent.compareTo(libellePrecedent.getDateCourante()) <= 0) {
etatTranche = 2;
} else {
etatTranche = 3;
}
}
else {
etatTranche = 1;
}
}
return etatTranche;
}
Upvotes: 0
Views: 344
Reputation: 450
The sollution is to add @spy @InjectMocks CalculeMajStockServiceImpl calculeMajStockServiceImpl;
Upvotes: 0
Reputation: 1820
This creates a spy instance of CalculeMajStockServiceImpl
without injecting mocks.
@Spy
CalculeMajStockServiceImpl calculeMajStockServiceImpl = Mockito.spy(CalculeMajStockServiceImpl.class);
You may try
@InjectMocks
CalculeMajStockServiceImpl calculeMajStockServiceImpl;
And then explicitly define a spy inside @Before
method.
Mockito does not support injecting mocks into spies.
Note: If you are using MockitoJUnitRunner
, you don't have explicitly call MockitoAnnotations.initMocks(this)
Upvotes: 1