在此测试上已经花费了很多时间,无法解释我的解决方法,我别无选择,只能寻求您的帮助:)

使用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块(第一个属性期望)上崩溃。如果我评论第一个,那么它将继续把它放在下一个。尝试对any,anyString进行参数匹配。

我的看法是,我模拟了JdbcConnectionProperties loadProperties,以便简化测试。然后,我将模拟的Properties对象传递到测试中。

然后...

...它应该工作。
顺便说一句,我从未在Exceptions块中看到过如此大的异常。

谢谢。

最佳答案

Hashtable#get是JMockit默认未模拟的几种方法之一,因为在模拟时它会干扰JDK或JMockit本身。您可以使用@Mocked("get")明确要求模拟该特定测试,以使其工作。

不过,在测试中仅使用实际的“ .properties”文件可能会更简单,而无需模拟。

关于java - 异常块上的JMockit NullPointerException?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7571284/

10-11 02:55