I'm setting up new java project , i have a void method in database class which returns single instance of the object (singleton class). I want to test it using junit and mockito.
I have tried the fallowing testcase code using assertSame which return's the test fails.
public class DatabaseConnectionTestCase {
DatabaseConnection connectoin;
@Before
public void setup()
{
connectoin = DatabaseConnection.getInstance();
}
@Test
public void test() throws SQLException
{
Assert.assertSame(connectoin.GetDBConnection(),connectoin.GetDBConnection());
}
}
Database class which is used for testing
public Connection con = null;
public static DatabaseConnection Database_single_instance = null;
// create single object for all the class
public static DatabaseConnection getInstance()
{
if (Database_single_instance == null)
Database_single_instance = new DatabaseConnection();
return Database_single_instance;
}
private DatabaseConnection()
{
}
// establish a database connection used across the project
public Connection GetDBConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/iteration";
String username = "root";
String password = "";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// return connection as single instance
public Connection getConnection() {
return con;
}
Aucun commentaire:
Enregistrer un commentaire