unit testing with wounit

27
Unit Testing with WOUnit Henrique Prange (HP)

Upload: wo-community

Post on 19-May-2015

434 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Unit Testing with WOUnit

Unit Testing with WOUnitHenrique Prange (HP)

Page 2: Unit Testing with WOUnit

• WOUnittest

• Wonder’s ERXTestCase

• JavaMemoryAdaptor

• Ad hoc Testing

History

Page 3: Unit Testing with WOUnit

Why another framework?

• Simple

• Fast

• Full support for Wonder features

• Concise assertions

• Test isolation

Page 4: Unit Testing with WOUnit

Features

• Use the new @Rule

• MockEditingContext extends ERXEditingContext

• Useful assertions for EOF logic

• Compatible with WOUnittest

Page 5: Unit Testing with WOUnit

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

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

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

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

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

@UnderTest

• Behave as a real object

• All validations apply

• Alternative to createAndInsertObject

Page 11: Unit Testing with WOUnit

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 12: Unit Testing with WOUnit

http://theinspirationroom.com

Page 13: Unit Testing with WOUnit

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

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

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

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

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

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

Restrictions

• No support for Properties

• No support for Localization

• No support for SQL operations

Page 21: Unit Testing with WOUnit

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

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

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

DEMO

Page 25: Unit Testing with WOUnit

Future

• Improved troubleshooting messaging

• Automatically load EOModels

• Fix for issue #20

Page 26: Unit Testing with WOUnit

License and Distribution• It’s free

• Apache 2 license

• Source and Binaries

• hprange.github.com/wounit

• github.com/hprange/wounit

• maven.wocommunity.org