How to mock the DB object?

731 views
Skip to first unread message

Doug Wikle

unread,
Sep 19, 2014, 9:56:18 AM9/19/14
to scalikejdbc...@googlegroups.com
Hi,

Is there anything built into ScalikeJDBC to help me mock the DB object and its functions?  The problem I am running into is that I have a function like this in my design which I want to unit test.

def doSomething(...): Unit = {

  DB.localTx { implicit session =>

    // Make a few calls to a DAO
      
  }
}

When I run the unit test I get the following error:

Connection pool is not yet initialized.(name:'default)
java.lang.IllegalStateException: Connection pool is not yet initialized.(name:'default)

In the unit test I would like the code within the transaction block to execute but without a real database connection.  I am using Mockito to mock the functions on the DAO.

We are considering wrapping the DB object with something we can mock, but before I go down that path I wanted to see if there was already a solution.

Any suggestions?

Thanks,
Doug

Kazuhiro Sera

unread,
Sep 30, 2014, 10:44:57 PM9/30/14
to scalikejdbc...@googlegroups.com
Hi,

Sorry for late reply. 

It's difficult to mock the DB companion object. 
If you need to replace the code, you can use scalikejbdc.NamedDB with ConnetionPool.DEFAULT_NAME.

class YourDAO {
  // you can replace this easily
  lazy val db = NamedDB(ConnectionPool.DEFAULT_NAME)
  def doSomething(...): Unit = {
    db.localTx { implicit session =>
      // Make a few calls to a DAO
    }
  }
}

val dao = new YourDAO {
  override lazy val db = mock[NamedDB]
}

Cheers,
-Kaz
Reply all
Reply to author
Forward
0 new messages