3. Why another framework?
• Simple
• Fast
• Full support for Wonder features
• Concise assertions
• Test isolation
4. Features
• Use the new @Rule
• MockEditingContext extends ERXEditingContext
• Useful assertions for EOF logic
• Compatible with WOUnittest
5. Writing Tests
• MockEditingContext annotated with @Rule
• Load the required EOModels before running tests
• Clear the environment after running each test
• Prepare EOs
• Play with EOs
• Use assertions to verify behavior
6. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
@Test
public void cantSaveFooWithNullProperty() {
Foo foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);
foo.setProperty(null);
confirm(foo, cannotBeSavedBecause("Foo must have a property"));
}
}
7. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
@Test
public void cantSaveFooWithNullProperty() {
Foo foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);
foo.setProperty(null);
confirm(foo, cannotBeSavedBecause("Foo must have a property"));
}
@Test
public void canSaveFooWithNotNullProperty() {
Foo foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);
foo.setProperty("bar");
confirm(foo, canBeSaved());
}
}
8. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
private Foo foo;
@Before
public void setup() {
foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);
}
@Test
public void cantSaveFooWithNullProperty() {
foo.setProperty(null);
confirm(foo, cannotBeSavedBecause("Foo must have a property"));
}
@Test
public void canSaveFooWithNotNullProperty() {
foo.setProperty("bar");
confirm(foo, canBeSaved());
}
}
9. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
@UnderTest
private Foo foo;
@Test
public void cantSaveFooWithNullProperty() {
foo.setProperty(null);
confirm(foo, cannotBeSavedBecause("Foo must have a property"));
}
@Test
public void canSaveFooWithNotNullProperty() {
foo.setProperty("bar");
confirm(foo, canBeSaved());
}
}
10. @UnderTest
• Behave as a real object
• All validations apply
• Alternative to createAndInsertObject
11. Assertions
• EOAssert class
• EO can/cannot be saved
• EO can/cannot be deleted
• EO has/hasn’t been saved
• EO has/hasn’t been deleted
• EC does/doesn’t save changes successfully
13. Dummy Objects
• Do not need to be initialized
• Because validations don’t apply
• @Dummy as alternative to ec.createSavedObject
14. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
@UnderTest
private Foo foo;
@Test
public void canSaveFooWithBar() {
Bar bar = ec.createSavedObject(Bar.class);
foo.setBar(bar);
confirm(foo, canBeSaved());
}
}
15. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
@UnderTest
private Foo foo;
@Dummy
private Bar bar;
@Test
public void canSaveFooWithBar() {
foo.setBar(bar);
confirm(foo, canBeSaved());
}
}
16. NSArray of EOs
• Useful to test toMany relationships
• Work with @UnderTest and @Dummy
• EOs can be spied with @Spy
17. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
@UnderTest
private Foo foo;
@Dummy
private Bar bar1, bar2, bar3, bar4;
@Test
public void cannotSaveFooWithLessThanFiveBars() {
foo.addToBarRelationship(bar1);
foo.addToBarRelationship(bar2);
foo.addToBarRelationship(bar3);
foo.addToBarRelationship(bar4);
confirm(foo, cannotBeSaved());
}
}
18. Sample
class FooTest {
@Rule
public MockEditingContext ec = new MockEditingContext("MyModel");
@UnderTest
private Foo foo;
@Dummy(size = 4)
private NSArray<Bar> bars;
@Test
public void cannotSaveFooWithLessThanFiveBars() {
foo.addObjectsToBothSidesOfRelationshipWithKey(bars, Foo.BARS_KEY);
confirm(foo, cannotBeSaved());
}
}
19. Restrictions
• No support for Properties
• No support for Localization
• No support for SQL operations