mercredi 27 avril 2016

How to mock the value returned from reading a properties file in jmock test

I have a singleton java properties class (RepositoryProperties.java) that reads from a text file to get the value returned by the readFromSql method:

public boolean readFromSql() {
    String lPhase = getPilotProfileDatabaseTransitionPhase();
    if (lPhase.equals("PHASE_ONE_SQL")) {
        return true;
    } else
        return false;
}

Then when I want to add a record via my repository implementation class, I have to check this value to know which code block to execute:

if (RepositoryProperties.getInstance().readFromSql()
      {

.. if true, write the data to SQL, else skip this block

Now I need to add a Jmock unit test for my repository add method such that readFromSql method returns true (another test will test the false value), without introducing a dependency on the text file. I am afraid jmock is completely new to me and I can't figure out how to mock the value returned by readFromSql. I don't have a setter method.

I tried adding an expectation for this, but it's not the correct approach (don't think this belongs in expectations) and my syntax is also wrong:

myMockContext.checking(new Expectations() {
{
        RepositoryProperties.getInstance().readFromSql();
        will(returnValue(true));

Would really appreciate a little guidance on how to do this properly and where.

Aucun commentaire:

Enregistrer un commentaire