lundi 23 novembre 2015

Mocking a static method

Below is the method i want to test. I am using TestNG framework for unit testing.

class Random{

    List<String> namesOfLinks;

    public List<String> methodIwantToTest(List<String> cktNames) {
            Map<String, Graph> maps =   DataBaseReader.getGraphs(cktNames);
            for (Entry<String, Graph> entry : maps.entrySet()) {
                graphList.add(entry.getValue().getName());
            }
    }

    return namesOfLinks;
}

I am writing the test cases for the method "methodIwantToTest" in the above class. I can provide some dummy cktNames and get the method to execute like below.

@Test (dataProvider = "dp")
public void test_methodIwantToTest(List<String> cktNames, List<String> expectedLinkNames){
    Random rm = new Random();
    List<String> actual = rm.methodIwantToTest(cktNames);
    Assert.assertEquals(actual,expectedLinkNames);
} 

Now comes the problem. When the actual method is executing when i invoke it on the 'rm' reference, it has a static method call to another API. It has to return something in order for my "method" to work. I searched the internet and found "easymock" as a solution. But i am unable to use "easyMock" to mock the static method (DataBaseReader.getGraphs()). I have to mock that method so that it returns a map of the defined type. Any suggestions would be great. Thanks !!

Aucun commentaire:

Enregistrer un commentaire