Mockito Mocked Class返回Null

时间:2013-09-21 02:00:11

标签: java unit-testing junit mockito

我正在使用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方法。 我检查了tableNameparentDirectoryPath在调用describeTable时的参数与我在initalize方法中定义的参数相同。

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

修改

我的测试方法类似于:

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

所以driver.main调用describeTable方法,我试图模仿它的行为。

编辑2

我描述的hive表类是:

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());

2 个答案:

答案 0 :(得分:2)

使用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

但看起来您无法控制DescribeHiveTable使用的DriverClass实例?如果是这样的话,那么

  • Mockito不会帮助你 - 或者你至少也要嘲笑DriverClass;或
  • 您必须使用反射将describeTable中的DriverClass替换为mockObj

答案 1 :(得分:1)

您可以使用模拟DriverClass初始化DescribeHiveTable(提供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());
}

}