由于MismatchReportingTestListener,单元测试失败

时间:2020-07-22 17:27:50

标签: java unit-testing mockito

我面临一个奇怪的问题,即我的单元测试失败/重试成功。我以前在包装中为2个类编写了单元测试用例,而当我为第三类编写了单元测试用例时,由于下面列出的错误,其他两个类的测试用例也开始失败

生成的测试报告中的错误:

错误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

错误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.

我认为与控制台相关的一些错误

  [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

奇怪的是,测试用例有时成功,有时却不成功。

我将粘贴引发问题的第三类和单元测试类的初始化片段

@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

单元测试

@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

我觉得 InjectMocks 的初始化中肯定存在一些问题,因为这是以前使用注入模型的测试类中的第一个类。希望能提供帮助。

0 个答案:

没有答案