Tuesday, December 13, 2011

Gradle is easy: Groovy tests

Today in Unit Testing, an evil framework class hid my important return value away in a private field.

Groovy to the rescue!

So far, all our code and tests are in Java. It took only two lines in build.gradle, as specified in the gradle documentation, and poof.

I put my little test class in src/test/groovy/package/structure, typed "gradle build" and wa-la! The results from the groovy test show up right along with the Java tests. Sweet.

class SomethingTest {
def email = "blah@blha.com";

UsefulClass usefulClass
CheckEmail subject

@Before
void setup() {
usefulClass = mock(UsefulClass.class);
subject = new Something(userService);
subject.setEmailAddress(email);
}

@Test
void falseWhenEmailFound() {
when(usefulClass.find(email)).thenReturn(new Thingie());

Reply<Boolean> result = subject.methodUnderTest();

assertEquals result.entity , Boolean.FALSE // see? that mean old private field is right there for the testing.
}

No comments:

Post a Comment