已经在这个测试上花费了很多时间并且无法推理出我的出路,我别无选择,只能寻求你的帮助:)
使用 JMockit 测试我自己的一些 JDBC“包装器”类,我走到了死胡同。
这是即时测试类:
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 块中看到过这种规模的异常。
谢谢你。