Java 由于不匹配的HreportingTestListener,单元测试失败

Java 由于不匹配的HreportingTestListener,单元测试失败,java,unit-testing,mockito,Java,Unit Testing,Mockito,我面临一个奇怪的问题,我的单元测试在重试时失败/成功。我之前在我的包中为两个类编写了单元测试用例,当我为第三个类编写单元测试用例时,由于下面列出的错误,其他两个类的测试用例也开始失败 生成的测试报告中出现错误: 错误1 Cannot instantiate @InjectMocks field named 'chunkedSnapshot' of type 'class com.XXXX.YYY.lambda.ChunkedSnapshot'. You haven't provided the

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

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

错误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
@Log4j2
公共类ChunkedSnapshot实现RequestHandler{
私有最终AmazonAthena AmazonAthena;
私人雅典娜克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克列克;
私有最终字符串快照日期;
私有最终字符串表名;
私有最终字符串数据库名;
私有最终字符串表;
私有最终字符串externalS3Location;
私人最终字符串工作组;
私有字符串可购买日期;
公共ChunkedSnapshot(){
这个(新的lambdac配置(System.getenv());
}
公共ChunkedSnapshot(LambdConfiguration LambdConfiguration){
这是(lambdaConfiguration.getAmazonAthena());
}
公共ChunkedSnapshot(AmazonAthena AmazonAthena){
this.amazonAthena=amazonAthena;
ConfigInitializer.initializeConfig();
snapshotDate=LocalDate.now().toString();
tableName=AppConfig.findString(ATHENA_MAIN_表);
databaseName=AppConfig.findString(ATHENA_数据库);
workGroup=AppConfig.findString(ATHENA_工作组);
currentTable=AppConfig.findString(当前_表);
externalS3Location=AppConfig.findString(表位置);
athenaQueryExecutor=新的athenaQueryExecutor();
}
@凌驾
公共字符串handleRequest(字符串事件、上下文){
//这里有一些内容
}…更多内容
单元测试

@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
@RunWith(MockitoJUnitRunner.class)
公共类ChunkedSnapshotTests扩展了AbstractTestCase{
@嘲弄
语境模拟语境;
@嘲弄
AthenaQueryExecutor mockAthenaQueryExecutor;
@嘲弄
AmazonAthena mockAmazonAthena;
@注射模拟
ChunkedSnapshot ChunkedSnapshot;
列表行列表;
列表数据列表;
String event=“一些测试事件”;
@以前
public void setup()引发InterruptedException{
行列表=新的ArrayList();
datumList=newarraylist();
行=新行();
基准基准1=新基准();
datum1.setVarCharValue(“某些val”);
基准基准2=新基准();
datum2.setVarCharValue(“某些val”);
datumList.add(datum1);
datumList.add(datum2);
行设置数据(datumList);
rowList.add(行);
当(mockAthenaQueryExecutor.processResultRows(any(),any())。然后返回(行列表);
}
@试验
公共void testHandlerRequest(){
handleRequest(事件,mockContext);
}
……更多内容
我觉得InjectMock的初始化一定有问题,因为这是以前使用InjectMock的测试类中的第一个类。如果有任何帮助,我们将不胜感激