productsInDatabse is a hashMap, output is enum, scanCode is from class which is tested
public static boolean isInDataBase(int code) {
return productsInDatabse.containsKey(code);
}
and I've got a test:
@Test
public void testScanCodeForCodeNotFound() {
Database db = Mockito.mock(Database.class);
when(db.isInDataBase(444)).thenReturn(false);
output = scanner.scanCode("444");
assertTrue(output == ProductProcessing.PRODUCT_NOT_FOUND);
}
But when()...then() returns with NPE. I saw examples when invoking was exactly like this. So my question is where is the bug, because I thought when()...then() works likes that.
Stacktrace:
Testcase: testScanCodeForCodeNotFound(iodevices.BarCodesScannerTest): Caused an ERROR
null
java.lang.NullPointerException
at database.Database.isInDataBase(Database.java:66)
at iodevices.BarCodesScannerTest.testScanCodeForCodeNotFound(BarCodesScannerTest.java:50)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
Test iodevices.BarCodesScannerTest FAILED
Remove the static
from your isInDataBase()
declaration.
See this if you can't remove the static: Mocking static methods with Mockito
You can't mock static
methods with Mockito; you'll need PowerMock for that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.