lundi 21 mars 2016

Mockito Java Test Always Passes - What am I doing wrong?

This is the code I want to test. The code runs fine, as I have the dependency.xml in resources (where it should be). It executes correctly.

@Component
public class ProjectBuilderBean {

public List<String> getDependencyList() {
    List<String> listDeps = new ArrayList<String>();

    try {
        ClassLoader classLoader = getClass().getClassLoader();
        File xmlFile = new File(classLoader.getResource("dependency.xml").getFile());
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(xmlFile);
        NodeList nList = doc.getElementsByTagName("dependency");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                String dependency = eElement.getElementsByTagName("artifactId").item(0).getTextContent();
                listDeps.add(dependency);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return listDeps;
    }
}

This is the test I've written and for some reason it always passes. I do not understand why and how it passes, but I know for a fact that it should not. I've added nothing to the list and it somehow still passes, even when I add it passes. Here is the test:

@WebAppConfiguration
public class ProjectBuilderBeanTest {

@Mock
private ProjectBuilderController projectBuilderBeanMock;

//Decleration of the Class Instance
@Mock
ProjectBuilderBean projectBuilderBean;

/**
 * @throws java.lang.Exception
 */
@Before
public void setUp() throws Exception {
    //Initialise the mocking of the class
    projectBuilderBean = Mockito.mock(ProjectBuilderBean.class);
}

@Test
public void getDependencyListTest() throws Exception {

    ArrayList<String> result = new ArrayList<String>();
    result.add("a");
    result.add("b");
    when(projectBuilderBean.getDependencyList()).thenReturn(result);

}

/**
 * @throws java.lang.Exception
 */
@After
public void tearDown() throws Exception {
    projectBuilderBean = null;
    }
}

Simply trying to test the consistency of the list generated through the dependency.xml file.

Here is a screenshot of the depenedency.xml: http://ift.tt/22zqzLC

Aucun commentaire:

Enregistrer un commentaire