Mockito Java测试总是通过 - 我做错了什么



这是我想要测试的代码。代码运行良好,因为我在资源(应该在的地方)中具有依赖.xml。它正确执行。

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

这是我写的测试,出于某种原因,它总是通过。我不明白它为什么以及如何通过,但我知道它不应该通过。我没有向列表中添加任何内容,即使我添加它通过了,它仍然以某种方式通过。这是测试:

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

只是尝试测试通过 dependcy.xml 文件生成的列表的一致性。

以下是去污的截图.xml:http://screenshot.net/3qoe4s0

只是为了回应评论者,但你的"测试"总是通过的原因是因为你没有测试任何东西。 你正在"执行"你的代码。 您没有断言任何副作用,也没有验证任何模拟调用。

最新更新