简体   繁体   English

Mockito Java测试始终通过-我做错了什么?

[英]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). 该代码运行良好,因为我在资源中(应位于的位置)具有dependency.xml。 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. 只需尝试测试通过dependency.xml文件生成的列表的一致性。

Here is a screenshot of the depenedency.xml: http://screenshot.net/3qoe4s0 这是depenedency.xml的屏幕截图: http ://screenshot.net/3qoe4s0

Just to echo the commenter, but the reason your "test" is always passing is because you're not testing anything. 只是为了回应评论者,但是您的“测试”始终通过的原因是因为您没有测试任何东西。 You're "exercising" your code. 您正在“执行”您的代码。 You're not asserting any side effects, you're not verifying any mock calls. 您没有断言任何副作用,也没有验证任何模拟调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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