Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you explain how the Exception is available in the test case?
    text
    copied!<p>There was a question regarding checking for a handled exception in Junits. I seem to have done this in my code. Other people tend to say it is not possible since the exception is not thrown by the method. Can someone explain what is happening in the code below.</p> <pre><code>public class DatabaseConnector { private DBConnectionInfo dbObject; private DBQueryStatements dbQueries; void loadConnectionInfo() { Properties databaseProperties = new Properties(); try { databaseProperties.load(getClass().getClassLoader().getResourceAsStream("database.properties")); dbObject.setDatabaseURL(databaseProperties.getProperty("jdbc.url")); dbObject.setUserName(databaseProperties.getProperty("jdbc.username")); dbObject.setPassword(databaseProperties.getProperty("jdbc.password")); dbObject.setDriver(databaseProperties.getProperty("jdbc.driver")); } catch (IOException e) { Logger lgr = Logger.getLogger(PostgreLocationManager.class.getName()); lgr.log(Level.SEVERE, e.getMessage(), e); } } public DBConnectionInfo connectionInit() { loadConnectionInfo(); try { Class.forName(dbObject.getDriver()); } catch (Exception e) { Logger lgr = Logger.getLogger(PostgreLocationManager.class.getName()); lgr.log(Level.SEVERE, e.getMessage(), e); } try { dbObject.setConnection(DriverManager.getConnection(dbObject.getDatabaseURL(), dbObject.getUserName(), dbObject.getPassword())); } catch (Exception e) { Logger lgr = Logger.getLogger(PostgreLocationManager.class.getName()); lgr.log(Level.SEVERE, e.getMessage(), e); } return dbObject; } } </code></pre> <p>The test cases for the above code.</p> <pre><code>public class DatabaseConnectorTest { DatabaseConnector dbConnector; DBConnectionInfo dbModelObject; DBQueryStatements dbQueries; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); dbModelObject = mock(DBConnectionInfo.class); dbQueries = mock(DBQueryStatements.class); dbConnector = new DatabaseConnector(dbModelObject,dbQueries); } @Test public void testDriverFailure() { when(dbModelObject.getDriver()).thenReturn("driver"); when(dbModelObject.getDatabaseURL()).thenReturn("jdbc:postgresql://127.0.0.1:5432/testdb"); when(dbModelObject.getUserName()).thenReturn("postgres"); when(dbModelObject.getPassword()).thenReturn("postgres"); try { dbConnector.connectionInit(); } catch (Exception e) { assertTrue(e instanceof ClassNotFoundException); } verify(dbModelObject).getDriver(); } @Test public void testConnectionFailure() { when(dbModelObject.getDriver()).thenReturn("org.postgresql.Driver"); when(dbModelObject.getDatabaseURL()).thenReturn("jdbc:postgresql://127.0.0.1:5432/testdb"); when(dbModelObject.getUserName()).thenReturn("uname"); when(dbModelObject.getPassword()).thenReturn("uname"); try { dbConnector.connectionInit(); } catch (Exception e) { assertTrue(e instanceof SQLException); } verify(dbModelObject).getDriver(); verify(dbModelObject).getDatabaseURL(); verify(dbModelObject).getUserName(); verify(dbModelObject).getPassword(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload