次のように、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とparentDirectoryPathwhendescribeTableが呼び出されていることを確認しましたが、initialize メソッドで定義したものと同じです。
ただし、まだ null の戻り値が返されます。この振る舞いがわかりません。多分私は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());