Specifying Expected Interactions in a Closuse

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Specifying Expected Interactions in a Closuse

David Hatton
Am curious as to why the following test does not pass (the transaction.commit line never seems to get executed::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable[] autoCloseables = [Mock(AutoCloseable), Mock(AutoCloseable)]
autoCloseables.collect { managedConnection.addActiveResource(it) }
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
autoCloseables.every {
1 * it.close()
}
}


But this test does::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable resourceOne = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceOne)
AutoCloseable resourceTwo = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceTwo)
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
1 * resourceOne.close()
1 * resourceTwo.close()
}


Am new to Groovy and Spock so I am probably doing something stupid...

Simplified form of the class under test attached...

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

ManagedConnection.java (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Specifying Expected Interactions in a Closuse

Przemysław Wojnowski
Hi.
IMHO you have to put "assert" explicite in closure: every { assert 1 * it.close() }.
See example here: http://farenda.com/spock/spock-framework-mock-and-closures

Cheers,
Przemek

2015-06-21 10:13 GMT+02:00 David Hatton <[hidden email]>:
Am curious as to why the following test does not pass (the transaction.commit line never seems to get executed::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable[] autoCloseables = [Mock(AutoCloseable), Mock(AutoCloseable)]
autoCloseables.collect { managedConnection.addActiveResource(it) }
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
autoCloseables.every {
1 * it.close()
}
}


But this test does::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable resourceOne = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceOne)
AutoCloseable resourceTwo = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceTwo)
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
1 * resourceOne.close()
1 * resourceTwo.close()
}


Am new to Groovy and Spock so I am probably doing something stupid...

Simplified form of the class under test attached...

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Specifying Expected Interactions in a Closuse

Peter Niederwieser
Administrator
In reply to this post by David Hatton
It’s explained here: http://spockframework.github.io/spock/docs/1.0/interaction_based_testing.html#_explicit_interaction_blocks

-Peter

On 21 Jun 2015, at 10:13, David Hatton <[hidden email]> wrote:

Am curious as to why the following test does not pass (the transaction.commit line never seems to get executed::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable[] autoCloseables = [Mock(AutoCloseable), Mock(AutoCloseable)]
autoCloseables.collect { managedConnection.addActiveResource(it) }
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
autoCloseables.every {
1 * it.close()
}
}


But this test does::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable resourceOne = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceOne)
AutoCloseable resourceTwo = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceTwo)
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
1 * resourceOne.close()
1 * resourceTwo.close()
}


Am new to Groovy and Spock so I am probably doing something stupid...

Simplified form of the class under test attached...


--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
<ManagedConnection.java>

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

signature.asc (859 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Specifying Expected Interactions in a Closuse

Kostis Kapelonis
In summary, to make your test pass use the following

interaction{
   autoCloseables.every {
   1 * it.close()
    }
}

The other solution mentioned: every { assert 1 * it.close() } will NOT work.

Kostis

On Mon, Jun 22, 2015 at 10:03 PM, Peter Niederwieser <[hidden email]> wrote:

> It’s explained here:
> http://spockframework.github.io/spock/docs/1.0/interaction_based_testing.html#_explicit_interaction_blocks
>
> -Peter
>
> On 21 Jun 2015, at 10:13, David Hatton <[hidden email]> wrote:
>
> Am curious as to why the following test does not pass (the
> transaction.commit line never seems to get executed::
>
> def "committing local transaction closes all active resources"() {
>     given:
>         AutoCloseable[] autoCloseables = [Mock(AutoCloseable),
> Mock(AutoCloseable)]
>         autoCloseables.collect { managedConnection.addActiveResource(it) }
>         LocalTransaction transaction =
> managedConnection.getLocalTransaction()
>         transaction.begin()
>     when:
>         transaction.commit()
>     then:
>         autoCloseables.every {
>             1 * it.close()
>         }
> }
>
>
>
> But this test does::
>
> def "committing local transaction closes all active resources"() {
>     given:
>         AutoCloseable resourceOne = Mock(AutoCloseable)
>         managedConnection.addActiveResource(resourceOne)
>         AutoCloseable resourceTwo = Mock(AutoCloseable)
>         managedConnection.addActiveResource(resourceTwo)
>         LocalTransaction transaction =
> managedConnection.getLocalTransaction()
>         transaction.begin()
>     when:
>         transaction.commit()
>     then:
>         1 * resourceOne.close()
>         1 * resourceTwo.close()
> }
>
>
>
> Am new to Groovy and Spock so I am probably doing something stupid...
>
> Simplified form of the class under test attached...
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Spock Framework - User" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> To post to this group, send email to [hidden email].
> Visit this group at http://groups.google.com/group/spockframework.
> For more options, visit https://groups.google.com/d/optout.
> <ManagedConnection.java>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Spock Framework - User" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> To post to this group, send email to [hidden email].
> Visit this group at http://groups.google.com/group/spockframework.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Specifying Expected Interactions in a Closuse

David Hatton
In reply to this post by Peter Niederwieser
Ok, makes sense now. Thanks for your help - and to all others who replied.

On Monday, June 22, 2015 at 8:03:42 PM UTC+1, Peter Niederwieser wrote:
It’s explained here: <a href="http://spockframework.github.io/spock/docs/1.0/interaction_based_testing.html#_explicit_interaction_blocks" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fspockframework.github.io%2Fspock%2Fdocs%2F1.0%2Finteraction_based_testing.html%23_explicit_interaction_blocks\46sa\75D\46sntz\0751\46usg\75AFQjCNHSfWlrjGOD9iPelIrOMr965VMsAg';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fspockframework.github.io%2Fspock%2Fdocs%2F1.0%2Finteraction_based_testing.html%23_explicit_interaction_blocks\46sa\75D\46sntz\0751\46usg\75AFQjCNHSfWlrjGOD9iPelIrOMr965VMsAg';return true;">http://spockframework.github.io/spock/docs/1.0/interaction_based_testing.html#_explicit_interaction_blocks

-Peter

On 21 Jun 2015, at 10:13, David Hatton <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="SLYaFQnJBf8J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">hatt...@...> wrote:

Am curious as to why the following test does not pass (the transaction.commit line never seems to get executed::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable[] autoCloseables = [Mock(AutoCloseable), Mock(AutoCloseable)]
autoCloseables.collect { managedConnection.addActiveResource(it) }
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
autoCloseables.every {
1 * it.close()
}
}


But this test does::

def "committing local transaction closes all active resources"() {
given:
AutoCloseable resourceOne = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceOne)
AutoCloseable resourceTwo = Mock(AutoCloseable)
managedConnection.addActiveResource(resourceTwo)
LocalTransaction transaction = managedConnection.getLocalTransaction()
transaction.begin()
when:
transaction.commit()
then:
1 * resourceOne.close()
1 * resourceTwo.close()
}


Am new to Groovy and Spock so I am probably doing something stupid...

Simplified form of the class under test attached...


--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="SLYaFQnJBf8J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">spockframewor...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="SLYaFQnJBf8J" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">spockfr...@googlegroups.com.
Visit this group at <a href="http://groups.google.com/group/spockframework" target="_blank" rel="nofollow" onmousedown="this.href='http://groups.google.com/group/spockframework';return true;" onclick="this.href='http://groups.google.com/group/spockframework';return true;">http://groups.google.com/group/spockframework.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.
<ManagedConnection.java>

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Specifying Expected Interactions in a Closuse

Przemysław Wojnowski
In reply to this post by Kostis Kapelonis
That quickie was is good direction, but not to the end. Next time I'll do better. ;-)

Thanks for correction!

2015-06-22 21:11 GMT+02:00 Kostis Kapelonis <[hidden email]>:
In summary, to make your test pass use the following

interaction{
   autoCloseables.every {
   1 * it.close()
    }
}

The other solution mentioned: every { assert 1 * it.close() } will NOT work.

Kostis

On Mon, Jun 22, 2015 at 10:03 PM, Peter Niederwieser <[hidden email]> wrote:
> It’s explained here:
> http://spockframework.github.io/spock/docs/1.0/interaction_based_testing.html#_explicit_interaction_blocks
>
> -Peter
>
> On 21 Jun 2015, at 10:13, David Hatton <[hidden email]> wrote:
>
> Am curious as to why the following test does not pass (the
> transaction.commit line never seems to get executed::
>
> def "committing local transaction closes all active resources"() {
>     given:
>         AutoCloseable[] autoCloseables = [Mock(AutoCloseable),
> Mock(AutoCloseable)]
>         autoCloseables.collect { managedConnection.addActiveResource(it) }
>         LocalTransaction transaction =
> managedConnection.getLocalTransaction()
>         transaction.begin()
>     when:
>         transaction.commit()
>     then:
>         autoCloseables.every {
>             1 * it.close()
>         }
> }
>
>
>
> But this test does::
>
> def "committing local transaction closes all active resources"() {
>     given:
>         AutoCloseable resourceOne = Mock(AutoCloseable)
>         managedConnection.addActiveResource(resourceOne)
>         AutoCloseable resourceTwo = Mock(AutoCloseable)
>         managedConnection.addActiveResource(resourceTwo)
>         LocalTransaction transaction =
> managedConnection.getLocalTransaction()
>         transaction.begin()
>     when:
>         transaction.commit()
>     then:
>         1 * resourceOne.close()
>         1 * resourceTwo.close()
> }
>
>
>
> Am new to Groovy and Spock so I am probably doing something stupid...
>
> Simplified form of the class under test attached...
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Spock Framework - User" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> To post to this group, send email to [hidden email].
> Visit this group at http://groups.google.com/group/spockframework.
> For more options, visit https://groups.google.com/d/optout.
> <ManagedConnection.java>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Spock Framework - User" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> To post to this group, send email to [hidden email].
> Visit this group at http://groups.google.com/group/spockframework.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.