异常阻止JMockit NullPointerException?

时间:2011-09-27 15:00:26

标签: java jmockit

我已经花了很多时间参加这项考试并且无法推理我的出路,除了寻求帮助之外我别无选择。)

使用JMockit测试我自己的一些JDBC“Wrapper”类,我走到了死胡同。

这是课程测试:

public class JdbcConnectionProperties {
    private Properties properties = new Properties();
    private String username;
    private String password;
    private String connectionString;

    public JdbcConnectionProperties(String propertiesFilePath) {
        loadProperties(propertiesFilePath);
    }

    public void setProperties() {
        username = properties.getProperty("user");
        password = properties.getProperty("password");

        String connectionType = properties.getProperty("connection_type");
        String serverAddress = properties.getProperty("server_address");
        String port = properties.getProperty("port");
        String sid = properties.getProperty("sid");

        //Create a connection string
        connectionString = "jdbc:oracle:" + connectionType + ":@" + serverAddress + ":" + port + ":" + sid;
    }


    private void loadProperties(String propertiesFilePath) {
        String filePath = Thread.currentThread().getContextClassLoader().getResource(propertiesFilePath).getFile();
        //Load properties from classpath
        try {
            properties.load(new FileInputStream(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getConnectionString() {
        return connectionString;
    }

    public Properties getProperties() {
        return properties;
    }
}

这是测试:

public class JdbcConnectionPropertiesTest {

    @Test
    public void testSetProperties(
//            @Mocked final Properties properties
    ) throws Exception {

        //Mock loadFilePath method so i dont end up mocking a ton of classes
        new MockUp<JdbcConnectionProperties>() {
            @Mock
            void loadProperties(String propertiesFilePath) {
                //Doing nothing, simple "stub" method
            }
        };

        JdbcConnectionProperties jdbcConnectionProperties = new JdbcConnectionProperties("bla");
//        Deencapsulation.setField(jdbcConnectionProperties, "properties", properties);
//        Mockit.stubOutClass(JdbcConnectionProperties.class, "loadProperties");
        final String username = "username";
        final String password = "password";
        final String connectionType = "thin";
        final String serverAddress = "localhost";
        final String port = "1521";
        final String sid = "orcl";
        String connectionString = "jdbc:oracle:" + connectionType + ":@" + serverAddress + ":" + port + ":" + sid;

        new Expectations() {
            @Mocked
            Properties properties;

            {
                properties.get("user");
                result = username;

                properties.get("password");
                result = password;

                properties.get("connection_type");
                result = connectionType;

                properties.get("server_address");
                result = serverAddress;

                properties.get("port");
                result = port;

                properties.get("sid");
                result = sid;
            }
        };

        jdbcConnectionProperties.setProperties();

        Assert.assertEquals("Incorrect user", username, jdbcConnectionProperties.getUsername());
        Assert.assertEquals("Incorrect password", password, jdbcConnectionProperties.getPassword());
        Assert.assertEquals("Incorrect connection string", connectionString, jdbcConnectionProperties.getConnectionString());
    }
}

一些笔记。我尝试使用Deencapsulation将模拟属性放入对象中(我将它们留在代码中注释)。

我试着用@Mocked注释嘲笑它。

我尝试使用stubOutClass对其进行存根。

这不是我写的第一个测试,但我相对来说是JMockit的新手。 我以前写过的测试从来没有像我这样头疼。我想我用JMockit写了大约20到30个测试,从来没有像这样的问题。

错误是(在所有提到的场景中):

java.lang.NullPointerException
    at java.util.Hashtable.get(Hashtable.java:335)
    at jdbc.JdbcConnectionPropertiesTest$2.<init>(JdbcConnectionPropertiesTest.java:49)
    at jdbc.JdbcConnectionPropertiesTest.testSetProperties(JdbcConnectionPropertiesTest.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

课程非常简单。测试应该非常简单。但不知何故,测试在Expectations块上崩溃(在第一个属性期望中)。如果我评论第一个,那么它继续把它扔到下一个。尝试任何,anyString参数匹配。

我看到它的方式,我模拟JdbcConnectionProperties loadProperties,所以我可以简化我的测试。然后我将一个模拟的Properties对象传递给测试。

然后......

......应该有用。 顺便说一句,我从来没有在例外块中看到过这种程度的例外情况。

谢谢。

2 个答案:

答案 0 :(得分:2)

Hashtable#get是JMockit默认模拟的一些方法之一,因为它可以在模拟时干扰JDK或JMockit本身。您可以通过使用@Mocked("get")明确要求对其进行模拟来使此特定测试工作。

在测试中使用实际的“.properties”文件可能更简单,但没有嘲弄。

答案 1 :(得分:0)

new Expectations() {
            @Mocked("getProperty")
            Properties properties;

            {
                properties.getProperty("user");
                result = username;

                properties.getProperty("password");
                result = password;

                properties.getProperty("connection_type");
                result = connectionType;

                properties.getProperty("server_address");
                result = serverAddress;

                properties.getProperty("port");
                result = port;

                properties.getProperty("sid");
                result = sid;
            }
        };

感谢Rogerio。正如他所指出的,原因是“内部”阶级嘲笑。要记住一个非常小的限制。

需要注意的其他课程是(我希望我能写出来):

Classes to have in mind while mocking with JMockit