Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • May 07 01:13
    jacattau commented #1288
  • May 02 14:08

    github-actions[bot] on gh-pages

    Publish docs/2.0-SNAPSHOT (compare)

  • May 02 14:07

    github-actions[bot] on gh-pages

    Publish javadoc/2.0-SNAPSHOT (compare)

  • May 02 14:01

    leonard84 on groovy-3.0.8

    (compare)

  • May 02 14:01

    leonard84 on master

    Upgrade groovy to 3.0.8 and som… (compare)

  • May 02 14:01
    leonard84 closed #1316
  • May 02 13:57
    codecov[bot] commented #1316
  • May 02 13:57
    codecov[bot] commented #1316
  • May 02 13:57
    codecov[bot] commented #1316
  • May 02 13:56
    codecov[bot] commented #1316
  • May 02 13:53
    codecov[bot] commented #1316
  • May 02 13:53
    codecov[bot] commented #1316
  • May 02 13:53
    codecov[bot] commented #1316
  • May 02 13:53
    codecov[bot] commented #1316
  • May 02 13:52
    codecov[bot] commented #1316
  • May 02 13:52
    codecov[bot] commented #1316
  • May 02 13:52
    codecov[bot] commented #1316
  • May 02 13:47
    codecov[bot] commented #1316
  • May 02 13:47
    leonard84 synchronize #1316
  • May 02 13:47

    leonard84 on groovy-3.0.8

    Fix implicit this conversion af… Merge branch 'master' into szpa… (compare)

Leonard Brünings
@leonard84

@sevenlist Maybe just refactor your ClassThatLogs into

class ClassThatLogs {
    private static final Logger LOG_INSTANCE = LoggerFactory.getLogger(ClassThatLogs.class);

    private final Logger log;

    ClassThatLogs() {
        log = LOG_INSTANCE;
    }

    ClassThatLogs(Logger logger) {
        log = logger;
    }
}

And just use the constructor to inject your mocked logger and just use log instead of LOG_INSTANCE in the class.
If you don't want to expose it in the constructor you could also use groovys ability to access private fields.

class ClassThatLogs {
    private static final Logger LOG_INSTANCE = LoggerFactory.getLogger(ClassThatLogs.class);

    private Logger log = LOG_INSTANCE;
}

To modify the log field from the spec.

IMHO this is much a much cleaner approach then the other variants.

Alexander Kriegisch
@kriegaex
Static fields are always difficult to test, but it is kind of a standard pattern for loggers. Some people might not even use it (the pattern) intentionally, adding loggers via Lombok or Groovy transformations. Either way, adding an additional non-static logger field just for injectability from tests, while working nicely of course, looks super ugly IMO - especially if you have to do it in many classes. I think the perfect solution does not exist, like I just said in the other thread about my own too.
3 replies
Configuring the log framework to use a testable logger (like Björn said) is surely the cleanest way with regard to not having to use any dirty tricks or extend the application code just for tests, but of course then the tests are more like integration tests with regard to the log framework not being "mocked away".
r-asou
@r-asou:matrix.org
[m]
hi guys, when I used Spock with SpringBoot to do Unit Test for Service layer. I got this error you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
But when I saw some example online, they didn't do any special settings
Alexander Kriegisch
@kriegaex
It would be helpful to share an MCVE, at least a full Spock specification (test class).
Björn Kautler
@Vampire
Björn Kautler
@Vampire

https://twitter.com/spockframework/status/1377558567130107906

We have been advised of a copyright issue with the Spock name, to avoid future problems in this regard we have decided to rebrand. Henceforth, the Spock Framework will be renamed to GWT Framework (Given-When-Then).
Read the full announcement here https://spockframework.org/rebrand-announcement.html

spartanhooah
@spartanhooah

I'm taking a class on Reactive Spring, and I've been able to use Spock no problem so far, but now there's a test using StepVerifier.withVirtualTime() and it passes no matter what. Here's the whole test:

def "With virtual time"() {
    given:
    VirtualTimeScheduler.getOrSet()
    def longFlux = Flux.interval(Duration.ofSeconds(1))
        .take(3)
        .log()

    expect:
    StepVerifier.withVirtualTime({ longFlux.log() })
        .expectSubscription()
        .thenAwait(Duration.ofSeconds(3))
        .expectNext(0L, 1L, 2L)
        .verifyComplete()
}

I also don't see the logs from the flux. Any ideas?

Leonard Brünings
@leonard84
IIRC you need to create the flux inside the withVirtualTime method, see https://www.baeldung.com/reactive-streams-step-verifier-test-publisher#3-testing-time-based-publishers as an example. Also you don't need VirtualTimeScheduler.getOrSet()AFAIK.
spartanhooah
@spartanhooah
That's true, don't need VirtualTimeScheduler.getOrSet(), thanks.
kwangomango
@kwangomango
Is it possible to put Spock conditions in an external helper class and still get Spock to raise ConditionNotSatisfiedException? Any asserts i have in external helper methods just get treated like regular Groovy asserts.
class Testy extends Specification {

    Helper helper = new Helper()

    void "testy"() {

        expect:
           verify()
           helper.verify()

    }


    void verify() {  
        assert 1 == 2
    }
}

class Helper {

    void verify() {
        assert 1 == 3
    }
}
Leonard Brünings
@leonard84
No it is not possible, Spock only transforms classes derived from Specification. You might have a pseudo-Specification without any actual tests and only helper methods though.
kwangomango
@kwangomango
Thanks Leonard, so you are saying to have my Helper class extend Specification too? I see that works.
Leonard Brünings
@leonard84
yes
kwangomango
@kwangomango
:thumbsup:
Leonard Brünings
@leonard84
You can't have external mock interactions though
kwangomango
@kwangomango
That's fine, i'm mostly using Spock for integration tests on apis.
If i make the external helper a closure, rather than a method, then it works just like a local method on Specification. Why is that?
class ClosureTest extends Specification {

    Helper helper = new Helper()

    void "testy"() {

        expect:
            verify.call()
    }

}

class Helper {

    Closure verify= { assert 1 == 2 }

}
Vahid Pazirandeh
@vahidpaz
What http client normally blends well with Spock website functional testing? I've looked at a few (httpbuilder, httpbuilder-ng) and I like their syntax (httpbuilder is nice) bit those projects are either abandoned or dormant. I'd like to use something actively support. thx
Leonard Brünings
@leonard84
you could use https://rest-assured.io/ although it has its own when-then methods
Vahid Pazirandeh
@vahidpaz
Yes I noticed it does. It looks to be an all-inclusive library, even allowing groovy syntax in strings (I guess that it eval()'s on). I'll play around and see if there's a happy middle-ground
Vahid Pazirandeh
@vahidpaz
@leonard84 - thank you for all the time/effort you put in for Spock. It's a real blessing for people around the world.
Leonard Brünings
@leonard84
thanks
Björn Kautler
@Vampire
@vahidpaz I heard and read very good things about Geb and it is nicely integrated with Spock too.
Leonard Brünings
@leonard84
Geb is for browser testing, I assumed the question was geared towards REST API testing
Björn Kautler
@Vampire
Which part of "website functional testing" makes you think so?
"i'm mostly using Spock for integration tests on apis" was a different user one message earlier
Leonard Brünings
@leonard84
Well, since the listed frameworks where http clients and not selenium/webdriver, I assumed testing should be done on the http/rest layer.
If you want browser tests, then sure Geb is the best choice for Spock.
Vahid Pazirandeh
@vahidpaz
@Vampire thank you for the info. Yes I was referring to rest api testing but when the time comes to sprinkle some web app UI testing (some call it user acceptance tests?) I will take a look at Geb. thx again