繁体   English   中英

Mockito模拟类返回Null

[英]Mockito Mocked Class returns Null

我正在使用Mockito在我的JUnit测试类中模拟一个类,如下所示:

@Before
public void initialize(){
    DescribeHiveTable mockObj = Mockito.mock(DescribeHiveTable.class);
    String tableName = "clslog_assessments";
    String parentDirectoryPath ="src/test/resources/TEST18/RunFiles";
    String[] mockFeaturesArray1 = {"user_id","event_id"};
    ArrayList<String> mockFeaturesList1 = new ArrayList<String> (Arrays.asList(mockFeaturesArray1));
    when(mockObj.describeTable(tableName, parentDirectoryPath)).thenReturn(mockFeaturesList1);

然后,我有了Test方法,该方法随后从内部调用describeTable方法。 我检查了参数: tableNameparentDirectoryPathdescribeTable被称为是相同我在initalize方法定义。

但是,我仍然得到一个空返回值。 我不了解这种行为。 也许我没有正确使用Mockito?

编辑

我的测试方法是这样的:

@Test
public void testComplexFeaturesExistingRun() {
String[] args = {masterConfigPath, runFilesPath, rootDir};
DriverClass driver = new DriverClass();
driver.main(args);
}

所以driver.main调用了describeTable方法,我正在尝试模拟其行为。

编辑2

我的描述蜂巢表类是:

public class DescribeHiveTable {

public ArrayList<String> describeTable(String tableName, String parentDirectoryPath){
    String hiveQuery = "'describe " + tableName + "';";
    String bashScriptFile = parentDirectoryPath + "/describeTable.sh";

    .
    .
    .
        final Process process = builder.start();
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while((line=br.readLine())!=null) {
            String[] output = line.split("\t");
            columnList.add(output[0]);
       }
       return columnList;

这就是我所说的describe table:

DescribeHiveTable describeTable;
describeTable = new DescribeHiveTable();
ArrayList<String> columnList = describeTable.describeTable(tableName, runFile.getParent());

使用Mockito的方法是

private DescribeHiveTable mockObj; // must be accessible to Test methods

@Before
public void initialize(){
    this.mockObj = Mockito.mock(DescribeHiveTable.class);
    <etc>
}

@Test
public void testComplexFeaturesExistingRun() {
    /* test the objects that are set up to use this.mockObj,
       and not the usual type of DescribeHiveTable */
}

注意

describeTable = new DescribeHiveTable();

表示您正在使用的是新的,未模拟的DescribeHiveTable ,而不是mockObj的嘲笑mockObj

但是,似乎您无法控制DriverClass所使用的DescribeHiveTable实例? 如果是这样的话

  • Mockito不会帮助您-否则您至少也必须嘲笑DriverClass 要么
  • 你必须使用反射来代替describeTableDriverClassmockObj

您可以使用DescribeHiveTable的模拟形式初始化DriverClass (提供的DescribeHiveTableDriverClass的实例变量),如下所示:

public class TestClass{

@Mock
DescribeHiveTable mockObj;
// This will create a new instance of DriverClass with a mock of DescribeHiveTable
@InjectMocks
DriverClass driver;

@Before
    public void init() {

        MockitoAnnotations.initMocks(this);

        tableName = "clslog_assessments";
        parentDirectoryPath = "src/test/resources/TEST18/RunFiles";
        mockFeaturesArray1 = new String[] { "user_id", "event_id" };
        mockFeaturesList1 = new ArrayList<String>(
                Arrays.asList(mockFeaturesArray1));
        when(mockObj.describeTable(tableName, parentDirectoryPath)).thenReturn(
                mockFeaturesList1);
}

@Test
public void test() {
    // when(methodCall)
    assertEquals(mockFeaturesList1, driver.main());
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM