我正在使用 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
从内部调用该方法。我检查了参数:tableName
以及parentDirectoryPath
何时describeTable
被调用与我在 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;
这就是我调用描述表的方式:
DescribeHiveTable describeTable;
describeTable = new DescribeHiveTable();
ArrayList<String> columnList = describeTable.describeTable(tableName, runFile.getParent());