简体   繁体   中英

Mockito Mocked Class returns Null

I am using Mockito to mock a class in my JUnit test class as follows:

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

Then I have my Test method, which subsequently calls the describeTable method from within. I checked that the arguments: tableName and parentDirectoryPath when describeTable is being called are same as those I have defined in the initalize method.

However, I still get a null return value. I don't understand this behavior. Maybe I'm not using Mockito correctly?

EDIT

My Test method is something like:

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

So driver.main calls the describeTable method, whose behavior I'm trying to mock.

EDIT 2

My describe hive table class is :

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;

This is how I'm calling describe table:

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

The way to use Mockito is

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 */
}

Note that

describeTable = new DescribeHiveTable();

means that you're using a new, unmocked, DescribeHiveTable , not the mocked mockObj .

But it looks like you don't have control over the DescribeHiveTable instance used by the DriverClass ? If that's the case then either

  • Mockito isn't going to help you -- or you at least have to mock the DriverClass too; or
  • you have to use reflection to replace the describeTable in DriverClass with mockObj .

You can initialize the DriverClass with a mock of DescribeHiveTable (provided DescribeHiveTable is an instance variable of DriverClass ) as below:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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