How to write a spock unit test case for traits in grails 2.4?

Rahul Pratik

I am using Traits for making my controllers DRY. I want to unit test the Trait class using Spock. Here is my sample trait and Spock test case respectively:

trait SomeTrait {
    public void checkSomething (Closure c ){
        // Do some operation
        c.call
    }
}

@TestMixin(GrailsUnitTestMixin)
class SomeTraitSpec extends Specification {
     void "test checkSomething "(){
        setup: 
        MockedClass mockedObj = new MockedClass()
        def x=0
        def c = {
            x=1
        }

        when:
        mockedObj.checkSomething(c)

        then:
        assert x==1
    }
 }
class MockedClass implements PermissionTrait {
     // some thing   
    }

Since trait is an interface, I have a Mocked class in my test case which is implementing the Trait, I create an object of this Mocked class and call my Trait method which I want to test. Is this the correct approach, if not please point in the right direction with an apt example .

Poundex

Groovy's type coercion can be used to add behaviours from a trait to a class at runtime.

class MyTraitSpec extends Specification
{
    @Shared
    MyTrait testInstance = new Object() as MyTrait

    // ...
}

You should be aware that this creates a proxied instance, although the documentation (http://docs.groovy-lang.org/docs/groovy-2.3.0/html/documentation/core-traits.html#_runtime_implementation_of_traits) says the proxy is guaranteed to implement the trait and any/all interfaces, this could cause problems if you're ever checking the concrete type of the object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

grails Unit test spock issue

From Dev

How to test the catch block code in Spock unit test Grails

From Dev

Grails: how to save value to transient field while unit test with spock?

From Dev

Grails: how to save value to transient field while unit test with spock?

From Java

Spock -Unit Test:How to write spock unit test for @around annotation which takes Mono

From Dev

Grails Spock Mock Injected Service in Unit Test

From Dev

Asserting view with Grails Spock Unit Test for Controllers

From Dev

How to write unit test case for BadRequest?

From Dev

How to unit test PHP traits

From Dev

how to assert values inside "render" in unit test case (grails ,junit)

From Dev

Grails Spock Unit Test - What is "1 *" and why "too few invocations"?

From Dev

Using Spock data driven tests in Grails 3 unit test

From Dev

How to test Grails service using Spock?

From Dev

Unit Test case in Grails with mocking the methods

From Dev

How to write mock unit test case for ExecuteNonQuery & ExecuteScalar & GetDataSet method

From Dev

How to write a unit test for a custom logger in log4j2

From Dev

Write integration test case for grails plugins

From Dev

How to write Unit Test for this class using Jersey 2 test framework

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

How to mock render in (Spock, Grails) unit testing Taglib?

From Dev

How to write a test case for this

From Dev

How to write this test case?

From Dev

write unit test case in python for below algorithm

From Dev

How do I configure mime types in a grails Spock test?

From Dev

How to use spock to test a service with real transactions in Grails application?

From Dev

How we write basic unit test in angular 2?

From Dev

How to use VndErrorJsonRenderer in grails unit test

From Dev

How to unit test logging error with Spock framework in groovy

From Dev

Spock Mock not working for unit test

Related Related

  1. 1

    grails Unit test spock issue

  2. 2

    How to test the catch block code in Spock unit test Grails

  3. 3

    Grails: how to save value to transient field while unit test with spock?

  4. 4

    Grails: how to save value to transient field while unit test with spock?

  5. 5

    Spock -Unit Test:How to write spock unit test for @around annotation which takes Mono

  6. 6

    Grails Spock Mock Injected Service in Unit Test

  7. 7

    Asserting view with Grails Spock Unit Test for Controllers

  8. 8

    How to write unit test case for BadRequest?

  9. 9

    How to unit test PHP traits

  10. 10

    how to assert values inside "render" in unit test case (grails ,junit)

  11. 11

    Grails Spock Unit Test - What is "1 *" and why "too few invocations"?

  12. 12

    Using Spock data driven tests in Grails 3 unit test

  13. 13

    How to test Grails service using Spock?

  14. 14

    Unit Test case in Grails with mocking the methods

  15. 15

    How to write mock unit test case for ExecuteNonQuery & ExecuteScalar & GetDataSet method

  16. 16

    How to write a unit test for a custom logger in log4j2

  17. 17

    Write integration test case for grails plugins

  18. 18

    How to write Unit Test for this class using Jersey 2 test framework

  19. 19

    How to mock render in (Spock, Grails) unit testing Taglib?

  20. 20

    How to mock render in (Spock, Grails) unit testing Taglib?

  21. 21

    How to write a test case for this

  22. 22

    How to write this test case?

  23. 23

    write unit test case in python for below algorithm

  24. 24

    How do I configure mime types in a grails Spock test?

  25. 25

    How to use spock to test a service with real transactions in Grails application?

  26. 26

    How we write basic unit test in angular 2?

  27. 27

    How to use VndErrorJsonRenderer in grails unit test

  28. 28

    How to unit test logging error with Spock framework in groovy

  29. 29

    Spock Mock not working for unit test

HotTag

Archive