Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JMockit NullPointerException on Exceptions block?
- 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());
- }
- }
- 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)
- 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;
- }
- };
Add Comment
Please, Sign In to add comment