Unit Testing with WOUnit

795 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
795
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit Testing with WOUnit

  1. 1. Unit Testing with WOUnitHenrique Prange (HP)
  2. 2. • WOUnittest• Wonder’s ERXTestCase• JavaMemoryAdaptor• Ad hoc TestingHistory
  3. 3. Why another framework?• Simple• Fast• Full support for Wonder features• Concise assertions• Test isolation
  4. 4. Features• Use the new @Rule• MockEditingContext extends ERXEditingContext• Useful assertions for EOF logic• Compatible with WOUnittest
  5. 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. 6. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@Testpublic void cantSaveFooWithNullProperty() {Foo foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);foo.setProperty(null);confirm(foo, cannotBeSavedBecause("Foo must have a property"));}}
  7. 7. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@Testpublic void cantSaveFooWithNullProperty() {Foo foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);foo.setProperty(null);confirm(foo, cannotBeSavedBecause("Foo must have a property"));}@Testpublic void canSaveFooWithNotNullProperty() {Foo foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);foo.setProperty("bar");confirm(foo, canBeSaved());}}
  8. 8. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");private Foo foo;@Beforepublic void setup() {foo = ERXEOControlUtilities.createAndInsertObject(ec, Foo.class);}@Testpublic void cantSaveFooWithNullProperty() {foo.setProperty(null);confirm(foo, cannotBeSavedBecause("Foo must have a property"));}@Testpublic void canSaveFooWithNotNullProperty() {foo.setProperty("bar");confirm(foo, canBeSaved());}}
  9. 9. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@UnderTestprivate Foo foo;@Testpublic void cantSaveFooWithNullProperty() {foo.setProperty(null);confirm(foo, cannotBeSavedBecause("Foo must have a property"));}@Testpublic void canSaveFooWithNotNullProperty() {foo.setProperty("bar");confirm(foo, canBeSaved());}}
  10. 10. @UnderTest• Behave as a real object• All validations apply• Alternative to createAndInsertObject
  11. 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
  12. 12. http://theinspirationroom.com
  13. 13. Dummy Objects• Do not need to be initialized• Because validations don’t apply• @Dummy as alternative to ec.createSavedObject
  14. 14. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@UnderTestprivate Foo foo;@Testpublic void canSaveFooWithBar() {Bar bar = ec.createSavedObject(Bar.class);foo.setBar(bar);confirm(foo, canBeSaved());}}
  15. 15. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@UnderTestprivate Foo foo;@Dummyprivate Bar bar;@Testpublic void canSaveFooWithBar() {foo.setBar(bar);confirm(foo, canBeSaved());}}
  16. 16. NSArray of EOs• Useful to test toMany relationships• Work with @UnderTest and @Dummy• EOs can be spied with @Spy
  17. 17. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@UnderTestprivate Foo foo;@Dummyprivate Bar bar1, bar2, bar3, bar4;@Testpublic void cannotSaveFooWithLessThanFiveBars() {foo.addToBarRelationship(bar1);foo.addToBarRelationship(bar2);foo.addToBarRelationship(bar3);foo.addToBarRelationship(bar4);confirm(foo, cannotBeSaved());}}
  18. 18. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@UnderTestprivate Foo foo;@Dummy(size = 4)private NSArray<Bar> bars;@Testpublic void cannotSaveFooWithLessThanFiveBars() {foo.addObjectsToBothSidesOfRelationshipWithKey(bars, Foo.BARS_KEY);confirm(foo, cannotBeSaved());}}
  19. 19. Restrictions• No support for Properties• No support for Localization• No support for SQL operations
  20. 20. http://www.wikipedia.org
  21. 21. Spying Objects• Useful to workaround WOUnit limitations• Allows to change and verify behavior• Requires Mockito• @Spy as alternative to spy(new Foo) + insertObject• Requires @RunWith(MockitoJUnitRunner.class)
  22. 22. Sampleclass FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@Testpublic void callSlowMethodWhenDoingSomething() {Foo foo = new Foo();foo = Mockito.spy(foo);ec.insertObject(foo);Mockito.doNothing().when(foo).slowMethodWithSideEffects();foo.doSomething();Mockito.verify(foo).slowMethodWithSideEffects();}}
  23. 23. Sample@RunWith(MockitoJUnitRunner.class)class FooTest {@Rulepublic MockEditingContext ec = new MockEditingContext("MyModel");@Spy @UnderTestprivate Foo foo;@Testpublic void callSlowMethodWhenDoingSomething() {Mockito.doNothing().when(foo).slowMethodWithSideEffects();foo.doSomething();Mockito.verify(foo).slowMethodWithSideEffects();}}
  24. 24. DEMO
  25. 25. Future• Improved troubleshooting messaging• Automatically load EOModels• Fix for issue #20
  26. 26. License and Distribution• It’s free• Apache 2 license• Source and Binaries• hprange.github.com/wounit• github.com/hprange/wounit• maven.wocommunity.org
  27. 27. Q&AHenrique Prange (HP)hprange@gmail.comtwitter.com/hprange

×