Groovy Testing

  • 3,191 views
Uploaded on

A short introduction to Groovy testing techniques

A short introduction to Groovy testing techniques

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,191
On Slideshare
0
From Embeds
0
Number of Embeds
8

Actions

Shares
Downloads
112
Comments
0
Likes
6

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Groovy testing Davide Rossi
    • Jug Milano, 19/02/2009
  • 2. Contents
    • - Groovy built-in testing framework
    • - Unit test in Groovy: GroovyTestCase
        • - Testing for exceptions
    • - Groovy Mock and Stub
        • - The Java way: easymock, jMock...
        • - The Groovy way: using Categories
        • - The Groovy way: using ExpandoMetaClass
        • - The Groovy way: using Expandos and Maps
        • - The Groovy way: using stubFor and mockFor
    • - Testing Grails Actions
    • - Conclusion
  • 3. Groovy testing framework
    • - Groovy can be used to test Groovy and Java code
    • - Groovy has built-in support for Junit framework
      • - GroovyTestCase extends Junit TestCase
      • - Adds many assertion methods (assertToString, AssertArrayEquals, shouldFail...)
    • - Assertions are parts of the language
      • ex:
        • def number = 2
        • assert (number + 2) == 4
  • 4. GroovyTestCase
    • class SimpleUnitTest extends GroovyTestCase {
    • void testSimple() {
    • def integerList = [1, 2, 3]
    • assertEquals 2, integerList[1]
    • assertEquals 6, integerList[0] + integerList[1] + integerList[2]
    • }
    • }
  • 5. GroovyTestCase: testing for exceptions
    • class ClassToTest {
    • def methodWhichThrowsException() {
    • // Doing some work...
    • throw new NumberFormatException("Warning!")
    • }
    • }
    • void testSimple() {
    • def objectToTest = new ClassToTest()
    • shouldFail(NumberFormatException) {
    • objectToTest.methodWhichThrowsException()
    • }
    • }
  • 6. Mocks and Stubs
    • - Mocks and stubs are used to substitute real collaborators of the tested class
    • - With mocks and stubs you can unit test your code in isolation without depend form external code
    • - stubs have weak expectations: verify state of mocked objects
    • - mocks have strong expectations: verify state and behaviour of mocked objects
  • 7. The Java way
    • - Easymock, jMock, jMockit, Mockito...
    • public void testGetPrice() {
    • CurChange mockedDependency = createMock(CurChange.class);
    • expect(mockedDependency.getPriceByEUR(EUR)).andReturn(new BigDecimal(100));
    • replay(mockedDependency);
    • BigDecimal price = systemUnderTest.getPrice(EUR);
    • assertNotNull(price);
    • verify(mockedDependency);
    • }
  • 8. The Groovy way
    • Groovy dynamic nature allows easy mocks implementation
    • - Mock using mockFor and stubFor
    • - Mock using Categories
    • - Mock using ExpandoMetaClass
    • - Mock using Expandos and Maps
  • 9. Mock using mockFor and stubFor
    • - Inspired by easyMock, with same power and semantic
    • - Similar to Java
    • - Can be used everywhere a mock is needed
    • - You define expectations and verify them
    • - Mock Groovy and Java Collaborators
    • - Behavior specified via Closures, allowing static or calculated return values, throwing exceptions, asserting argument values...
    • - Not dependent on any external mock library
  • 10. StubFor and mockFor
    • def save = {
    • try {
    • def file = new File(baseDir, params.id)
    • file.text = params.text
    • render "success"
    • } catch (Exception e) {
    • render "exception ${e.message}"
    • }
    • }
  • 11. stubFor
    • void testSave() {
    • def testObj = new ClassToTest()
    • def fileMock = new StubFor(java.io.File)
    • def resultText
    • fileMock.demand.setText() { resultText = it }
    • fileMock.demand.close {}
    • fileMock.use {
    • testObj.save()
    • }
    • assertEquals "Qui va il contenuto del file" , resultText
    • }
  • 12. mockFor
    • def save = {
    • def params = ['baseDir': '.', 'id': 'NuovoFile', 'text': 'Qui va il contenuto del file']
    • try {
    • def file = new File(params.baseDir, params.id)
    • file.text = params.text
    • file.text = "Qui aggiungo testo"
    • file.close()
    • } catch (Exception e) {
    • log "exception ${e.message}"
    • }
    • }
  • 13. mockFor
    • void testSave() {
    • def testObj = new ClassToTest()
    • def fileMock = new MockFor(java.io.File)
    • def resultText1, resultText2
    • fileMock.demand.setText() { resultText1 = it }
    • fileMock.demand.setText() { resultText2 = it }
    • fileMock.demand.close(1..1) {}
    • fileMock.use {
    • testObj.saveForMock()
    • }
    • assertEquals "Qui va il contenuto del file" , resultText1
    • assertEquals "Qui aggiungo testo" , resultText2
    • }
  • 14. Mocking using Categories
    • What are Categories ? A MOP method injection technique
    • class StringStrangeFormatUtil {
    • def static specialFormat(String self) {
    • if (self.size() < 10) {
    • return &quot;***${self}***&quot;
    • }
    • }
    • }
    • use(StringStrangeFormatUtil) {
    • println &quot;ciao&quot;.specialFormat()
    • }
  • 15. Mocking using Categories
    • def methodForMockUsingCategoriesOrMetaclass() {
    • int value = collaboratorMethod() * 3
    • return value
    • }
    • def collaboratorMethod() {
    • // Attendere prego...
    • // Please Wait...
    • return 10
    • }
  • 16. Mocking using Categories
    • class TestMockUsingCategories extends GroovyTestCase {
    • void testMethodForMockUsingCategories() {
    • def testObj = new ClassToTest()
    • use(MockHelper) {
    • def result = testObj.methodForMockUsingCategoriesOrMetaclass()
    • assertEquals 399, result
    • }
    • }
    • }
    • class MockHelper {
    • def static collaboratorMethod(ClassToTest self) { 133 }
    • }
  • 17. Mocking using ExpandoMetaClass
    • void testMethodForMockUsingCategoriesOrMetaclass() {
    • def emc = new ExpandoMetaClass(ClassToTest)
    • emc.collaboratorMethod = { -> 133 }
    • emc.initialize()
    • def testObj = new ClassToTest()
    • testObj.metaClass = emc
    • def result = testObj.methodForMockUsingCategoriesOrMetaclass()
    • assertEquals 399, result
    • }
  • 18. Mocking using Expandos
    • def saveFile(file) {
      • file.write &quot;Testo del file.&quot;
    • }
    • void testSaveFile() {
    • def fileMock = new Expando(text: &quot;, write: {text = &quot;Testo dinamico dell'Expando&quot; })
    • def testObj = new ClassToTest()
    • testObj.saveFile(fileMock)
    • assertEquals &quot;Testo dinamico dell'Expando&quot; , fileMock.text
    • }
  • 19. Mocking using Maps
    • void testSaveFile() {
    • def text = ''
    • def fileMock = [write : { text = &quot;Testo dinamico della mappa&quot; }]
    • def testObj = new ClassToTest()
    • testObj.saveFile(fileMock)
    • assertEquals &quot;Testo dinamico della mappa&quot; , text
    • }
  • 20. Testing Grails Actions
    • - Integration test: Grails uses a HSQL DB and injects environment objects (session, controllers...)
    • def team1, team2, team3, team4
    • void setUp() {
    • team1 = new Team(name: 'Varese', championship: 'Serie C2', city: 'Varese', supporters: 1000, foundationYear: 1910).save()
    • ....
    • }
    • void tearDown() {
    • Team.list()*.delete()
  • 21. Testing Grails Action
    • void testSearch() {
    • def tc = new TeamController()
    • tc.params.championship = &quot;Serie C2 &quot;
    • tc.search()
    • assertEquals &quot;/team/list&quot;, tc.modelAndView.viewName
    • assertEquals 1, tc.modelAndView.model.teamCount
    • assertTrue tc.modelAndView.model.teamList.contains(team1)
    • }
  • 22. Conclusions
    • - Some techniques (like Expandos or Categories) are useful when testing Groovy classes
    • - Mocking using maps is useful when testing Java classes but you have to pass the mocked object to the Class Under Test
    • - mockFor and stubFor are very powerful and are used as an alternative to frameworks like easymock, jMock...
    • - more frameworks are coming (Gmock)
    • - The future: Behavior driven development (BDD) ? easyB...
  • 23. Reference
    • - Groovy in Action (D. Konig, A. Glover, P. King, G. Laforge, J. Skeet) – Manning
    • - Programming Groovy – Dynamic productivity for the Java developer (Venkat Subramaniam) – Pragmatic Programmera
    • - http://groovy.codehaus.org/Testing+Guide