Saifuddin Merchant
Saifuddin Merchant

Reputation: 1171

Why is my JMockit test case not failing when I don't provide the correct expectations on a Strict Expectation?

I am using Jmockit for the firs time so I could be missing something trivial. I have a method under test addTopTenListsTest() which calls --> mockObjectifyInstance.save().entity(topTenList).now();

Objectify is mocked, however when I comment out the mockObjectifyInstance.save() call from the Expectations() (Strict Expectation) (shown in code block below) the test case still goes green. I was expecting the test case to fail, since a call would be made on a mocked object that is not listed in the Strict expectation.

Any suggestions?

@RunWith(JMockit.class)
public class DataManagerTest {

  @Mocked
  ObjectifyService mockObjectifyServiceInstance;

  @Mocked
  Objectify mockObjectifyInstance;

  @Test
  public void addTopTenListsTest() {

  final TopTenList topTenList = new TopTenList();

  new Expectations() {
    {

    ObjectifyService.ofy();
    result = mockObjectifyInstance;

    // mockObjectifyInstance.save().entity(topTenList).now(); --> expected test case to fail when this is commented out
    }
  };

  DataManager datamanager = new DataManager();
  datamanager.addTopTenList(topTenList);

  new Verifications() {{
    mockObjectifyInstance.save().entity(topTenList).now();
  }};
  }
}

Upvotes: 0

Views: 555

Answers (1)

Saifuddin Merchant
Saifuddin Merchant

Reputation: 1171

I figured out what I was doing wrong. When using strict expectations, I still need an empty verification block so that JMockIt knows when the verification phase starts.

So, if I remove the verification from the "verification" block, the code will fail. Which mean I can add the verification code in either the strict assertion block or in the verification block

@RunWith(JMockit.class)
public class DataManagerTest {

  @Mocked
  ObjectifyService mockObjectifyServiceInstance;

  @Mocked
  Objectify mockObjectifyInstance;

  @Test
  public void addTopTenListsTest() {

    final TopTenList topTenList = new TopTenList();

    new Expectations() {
    {

      ObjectifyService.ofy();
      result = mockObjectifyInstance;
     // mockObjectifyInstance.save().entity(topTenList).now(); --> expected test case to fail when this is commented out
    }
 };

 DataManager datamanager = new DataManager();
 datamanager.addTopTenList(topTenList);

 new Verifications() {{
   //leave empty for the strict assertion to play in!
 }};
}

}

Upvotes: 0

Related Questions