I am facing a weird issue where my unit tests are failing/succeeding on retrying. I previously wrote unit test case for 2 classes in my package and when I wrote the unit test cases for third class the test cases for the other two classes also start failing due to errors listed below
Errors in generated test Report:
Error 1
Cannot instantiate @InjectMocks field named 'chunkedSnapshot' of type 'class
com.XXXX.YYY.lambda.ChunkedSnapshot'. You haven't provided the instance at field declaration so
I tried to construct the instance. However the constructor or the initialization block threw an
exception : null
Error 2
Problems adding Mockito listener. Listener of type 'MismatchReportingTestListener' has already
been added and not removed. It indicates that previous listener was not removed according to the
API. When you add a listener, don't forget to remove the listener afterwards: Mockito.framework().removeListener(myListener); For more information, see the javadoc for
RedundantListenerException class.
Some error in console which I feel might be relevant
[junit] Cannot instantiate @InjectMocks field named 'chunkedSnapshot' of type 'class com.XXXX.YYYY.lambda.ChunkedSnapshot'.
[junit] You haven't provided the instance at field declaration so I tried to construct the instance.
[junit] However the constructor or the initialization block threw an exception : null
The weird thing is that the test cases sometimes succeed, sometimes do not.
I will paste down the initialization piece of the third class and unit test class for same which is causing this issue
@Log4j2
public class ChunkedSnapshot implements RequestHandler<String, String> {
private final AmazonAthena amazonAthena;
private AthenaQueryExecutor athenaQueryExecutor;
private final String snapshotDate;
private final String tableName;
private final String databaseName;
private final String currentTable;
private final String externalS3Location;
private final String workGroup;
private String buyableDate;
public ChunkedSnapshot() {
this(new LambdaConfiguration(System.getenv()));
}
public ChunkedSnapshot(LambdaConfiguration lambdaConfiguration) {
this(lambdaConfiguration.getAmazonAthena());
}
public ChunkedSnapshot(AmazonAthena amazonAthena) {
this.amazonAthena = amazonAthena;
ConfigInitializer.initializeConfig();
snapshotDate = LocalDate.now().toString();
tableName = AppConfig.findString(ATHENA_MAIN_TABLE);
databaseName = AppConfig.findString(ATHENA_DATABASE);
workGroup = AppConfig.findString(ATHENA_WORKGROUP);
currentTable = AppConfig.findString(CURRENT_TABLE);
externalS3Location = AppConfig.findString(TABLE_S3_LOCATION);
athenaQueryExecutor = new AthenaQueryExecutor();
}
@Override
public String handleRequest(String event, Context context) {
//some content here
}...more content
Unit Test
@RunWith(MockitoJUnitRunner.class)
public class ChunkedSnapshotTests extends AbstractTestCase {
@Mock
Context mockContext;
@Mock
AthenaQueryExecutor mockAthenaQueryExecutor;
@Mock
AmazonAthena mockAmazonAthena;
@InjectMocks
ChunkedSnapshot chunkedSnapshot;
List<Row> rowList;
List<Datum> datumList;
String event = "some test event";
@Before
public void setup() throws InterruptedException {
rowList = new ArrayList<>();
datumList = new ArrayList<>();
Row row = new Row();
Datum datum1 = new Datum();
datum1.setVarCharValue("some val");
Datum datum2 = new Datum();
datum2.setVarCharValue("some val");
datumList.add(datum1);
datumList.add(datum2);
row.setData(datumList);
rowList.add(row);
when(mockAthenaQueryExecutor.processResultRows(any(), any())).thenReturn(rowList);
}
@Test
public void testHandleRequest() {
chunkedSnapshot.handleRequest(event, mockContext);
}
..... more content
I feel there must be some problem in the initialization of the InjectMocks since this is the first class out of previous test classes which is using the inject mocks.Any help would be appreciated.