I was writing a test:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MedicineHeadServiceTest {
@Mock
MedicineHeadRepository medicineHeadRepositoryMock;
@Mock
MedicineHead medicineHeadTest;
@Autowired
private MedicineHeadService medicineHeadService;
@Test
public void buildMedicineId_if_not_in_db_Test() {
MedicineHead testObj = new MedicineHead();
testObj.setMedicineName("medicine");
testObj.setMedicineType(MedicineTypes.PILL);
when(medicineHeadRepositoryMock.getByMedicineId(any())).thenReturn(isNull());
String medicineId = medicineHeadService.buildMedicineId(testObj);
assertEquals("MEDICINE_PILL_0", medicineId);
but I have InvalidUseOfMatchersException... cannot evaluate...toString() on: when(medicineHeadRepositoryMock.getByMedicineId(any())).thenReturn(isNull());
method i want to test:
StringBuilder medicineId = new StringBuilder();
medicineId.append(medicineHead.getMedicineName().toUpperCase() + "_" + medicineHead.getMedicineType().name());
if (medicineHeadRepository.getByMedicineId(medicineId.toString() + "_0") != null) {
boolean finish = false;
int counter = 0;
while (finish != true) {
if(medicineHeadRepository.getByMedicineId(medicineId.toString() + "_" + counter) == null){
finish = true;
medicineId.append("_" + counter++);
} else {
counter++;
}
}
} else {
medicineId.append("_0");
}
return medicineId.toString();
}
Do You know how to fix this ? And do you know how i can run tests faster ? with
@RunWith(SpringRunner.class) @SpringBootTest
it works but It takes some time start.
Thanks in advance