Guest User

Untitled

a guest
Aug 22nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. JMockit NullPointerException on Exceptions block?
  2. public class JdbcConnectionProperties {
  3. private Properties properties = new Properties();
  4. private String username;
  5. private String password;
  6. private String connectionString;
  7.  
  8. public JdbcConnectionProperties(String propertiesFilePath) {
  9. loadProperties(propertiesFilePath);
  10. }
  11.  
  12. public void setProperties() {
  13. username = properties.getProperty("user");
  14. password = properties.getProperty("password");
  15.  
  16. String connectionType = properties.getProperty("connection_type");
  17. String serverAddress = properties.getProperty("server_address");
  18. String port = properties.getProperty("port");
  19. String sid = properties.getProperty("sid");
  20.  
  21. //Create a connection string
  22. connectionString = "jdbc:oracle:" + connectionType + ":@" + serverAddress + ":" + port + ":" + sid;
  23. }
  24.  
  25.  
  26. private void loadProperties(String propertiesFilePath) {
  27. String filePath = Thread.currentThread().getContextClassLoader().getResource(propertiesFilePath).getFile();
  28. //Load properties from classpath
  29. try {
  30. properties.load(new FileInputStream(filePath));
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public String getUsername() {
  37. return username;
  38. }
  39.  
  40. public String getPassword() {
  41. return password;
  42. }
  43.  
  44. public String getConnectionString() {
  45. return connectionString;
  46. }
  47.  
  48. public Properties getProperties() {
  49. return properties;
  50. }
  51. }
  52.  
  53. public class JdbcConnectionPropertiesTest {
  54.  
  55. @Test
  56. public void testSetProperties(
  57. // @Mocked final Properties properties
  58. ) throws Exception {
  59.  
  60. //Mock loadFilePath method so i dont end up mocking a ton of classes
  61. new MockUp<JdbcConnectionProperties>() {
  62. @Mock
  63. void loadProperties(String propertiesFilePath) {
  64. //Doing nothing, simple "stub" method
  65. }
  66. };
  67.  
  68. JdbcConnectionProperties jdbcConnectionProperties = new JdbcConnectionProperties("bla");
  69. // Deencapsulation.setField(jdbcConnectionProperties, "properties", properties);
  70. // Mockit.stubOutClass(JdbcConnectionProperties.class, "loadProperties");
  71. final String username = "username";
  72. final String password = "password";
  73. final String connectionType = "thin";
  74. final String serverAddress = "localhost";
  75. final String port = "1521";
  76. final String sid = "orcl";
  77. String connectionString = "jdbc:oracle:" + connectionType + ":@" + serverAddress + ":" + port + ":" + sid;
  78.  
  79. new Expectations() {
  80. @Mocked
  81. Properties properties;
  82.  
  83. {
  84. properties.get("user");
  85. result = username;
  86.  
  87. properties.get("password");
  88. result = password;
  89.  
  90. properties.get("connection_type");
  91. result = connectionType;
  92.  
  93. properties.get("server_address");
  94. result = serverAddress;
  95.  
  96. properties.get("port");
  97. result = port;
  98.  
  99. properties.get("sid");
  100. result = sid;
  101. }
  102. };
  103.  
  104. jdbcConnectionProperties.setProperties();
  105.  
  106. Assert.assertEquals("Incorrect user", username, jdbcConnectionProperties.getUsername());
  107. Assert.assertEquals("Incorrect password", password, jdbcConnectionProperties.getPassword());
  108. Assert.assertEquals("Incorrect connection string", connectionString, jdbcConnectionProperties.getConnectionString());
  109. }
  110. }
  111.  
  112. java.lang.NullPointerException
  113. at java.util.Hashtable.get(Hashtable.java:335)
  114. at jdbc.JdbcConnectionPropertiesTest$2.<init>(JdbcConnectionPropertiesTest.java:49)
  115. at jdbc.JdbcConnectionPropertiesTest.testSetProperties(JdbcConnectionPropertiesTest.java:44)
  116. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  117. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
  118. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
  119. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
  120. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  121.  
  122. new Expectations() {
  123. @Mocked("getProperty")
  124. Properties properties;
  125.  
  126. {
  127. properties.getProperty("user");
  128. result = username;
  129.  
  130. properties.getProperty("password");
  131. result = password;
  132.  
  133. properties.getProperty("connection_type");
  134. result = connectionType;
  135.  
  136. properties.getProperty("server_address");
  137. result = serverAddress;
  138.  
  139. properties.getProperty("port");
  140. result = port;
  141.  
  142. properties.getProperty("sid");
  143. result = sid;
  144. }
  145. };
Add Comment
Please, Sign In to add comment