samedi 2 mai 2015

How to write a test in Java that checks if an object is being used in JMock or JUnit?

I'm writing some tests for a Java program, and I need to test if my object is added into a List x amount of times.

Below is a hypothetical code snippet....

ObjectWithList objectWithList = new ObjectWithList();    

JMock context = new JMock();
ObjectInterface oInterfaceMock = (ObjectInterface)context.mock(ObjectInterface.class);

context.checking(new Expectations{{
    oneOf(oInterfaceMock).makeSureIamOnlyStoredSomewhereOnce();
}});

context.assertIsSatisfied(); // This will be successful if objectWithList calls add only 
//once. 

I don't know if JUnit has a better way of testing this, any help would be much appreciated. I've been trying to do google searches, and searching through previous questions on Stack but I've had no luck.

Thank you so much everyone, any help is always appreciated!!

Aucun commentaire:

Enregistrer un commentaire