Unit Testing with WOUnit
-
Upload
wo-community -
Category
Technology
-
view
434 -
download
5
Transcript of Unit Testing with WOUnit
![Page 1: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/1.jpg)
Unit Testing with WOUnitHenrique Prange (HP)
![Page 2: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/2.jpg)
• WOUnittest
• Wonder’s ERXTestCase
• JavaMemoryAdaptor
• Ad hoc Testing
History
![Page 3: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/3.jpg)
Why another framework?
• Simple
• Fast
• Full support for Wonder features
• Concise assertions
• Test isolation
![Page 4: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/4.jpg)
Features
• Use the new @Rule
• MockEditingContext extends ERXEditingContext
• Useful assertions for EOF logic
• Compatible with WOUnittest
![Page 5: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/5.jpg)
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
![Page 6: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/6.jpg)
Sampleclass 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")); }}
![Page 7: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/7.jpg)
Sampleclass 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()); }}
![Page 8: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/8.jpg)
Sampleclass 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()); }}
![Page 9: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/9.jpg)
Sampleclass 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()); }}
![Page 10: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/10.jpg)
@UnderTest
• Behave as a real object
• All validations apply
• Alternative to createAndInsertObject
![Page 11: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/11.jpg)
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
![Page 13: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/13.jpg)
Dummy Objects
• Do not need to be initialized
• Because validations don’t apply
• @Dummy as alternative to ec.createSavedObject
![Page 14: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/14.jpg)
Sampleclass 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()); }}
![Page 15: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/15.jpg)
Sampleclass 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()); }}
![Page 16: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/16.jpg)
NSArray of EOs
• Useful to test toMany relationships
• Work with @UnderTest and @Dummy
• EOs can be spied with @Spy
![Page 17: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/17.jpg)
Sampleclass 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()); }}
![Page 18: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/18.jpg)
Sampleclass 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()); }}
![Page 19: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/19.jpg)
Restrictions
• No support for Properties
• No support for Localization
• No support for SQL operations
![Page 21: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/21.jpg)
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)
![Page 22: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/22.jpg)
Sampleclass FooTest { @Rule public MockEditingContext ec = new MockEditingContext("MyModel");
@Test public void callSlowMethodWhenDoingSomething() { Foo foo = new Foo(); foo = Mockito.spy(foo); ec.insertObject(foo);
Mockito.doNothing().when(foo).slowMethodWithSideEffects();
foo.doSomething();
Mockito.verify(foo).slowMethodWithSideEffects(); }}
![Page 23: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/23.jpg)
Sample@RunWith(MockitoJUnitRunner.class)class FooTest { @Rule public MockEditingContext ec = new MockEditingContext("MyModel");
@Spy @UnderTest private Foo foo;
@Test public void callSlowMethodWhenDoingSomething() { Mockito.doNothing().when(foo).slowMethodWithSideEffects();
foo.doSomething();
Mockito.verify(foo).slowMethodWithSideEffects(); }}
![Page 24: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/24.jpg)
DEMO
![Page 25: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/25.jpg)
Future
• Improved troubleshooting messaging
• Automatically load EOModels
• Fix for issue #20
![Page 26: Unit Testing with WOUnit](https://reader036.fdocuments.net/reader036/viewer/2022081403/555a6a46d8b42a972b8b4ae0/html5/thumbnails/26.jpg)
License and Distribution• It’s free
• Apache 2 license
• Source and Binaries
• hprange.github.com/wounit
• github.com/hprange/wounit
• maven.wocommunity.org