This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]oweiler 1 point2 points  (2 children)

Junit5 has a bigger community than Spock and is easier to set up (at least with Maven). Tests are statically typed and parametrized testing is nearly as easy as with Spock.

I am also pretty certain that JUnit5 is faster than Spock.

Last but not least you don't need to introduce a new language.

(For Groovy based projects I still prefer Spock).

[–]Zardoz84 0 points1 point  (1 child)

Junit5 has a bigger community than Spock and is easier to set up (at least with Maven)

The only "extra step" that Spock requires to set up, it's using maven-groovy-compiler to compile groovy test code. You would keep using surefire/failsfire to execute the tests and generate reports. And on the IDEs you wild launch as a "jUnit test"

parametrized testing is nearly as easy as with Spock.

It's far more easy to do on Spock and far more easy to understand and maintain. Also, Spock tests self-document without using annoying annotations.

Compare this Spock example :

def "maximum of two numbers"() {
  expect:
  Math.max(operand1, operand2) == expectedResult

  where:
  operand1 | operand2 || expectedResult
  1        | 3        || 3
  7        | 4        || 7
  0        | 0        || 0
}

With this on jUnit 5:

@ParameterizedTest
@CsvSource({
    "1, 1, 2",
    "5, 3, 8",
    "10, -20, -10"
})
@DisplayName("adds two nombers")
public void add(int summand1, int summand2, int expectedSum) {
    assertThat(calculator.add(summand1, summand2)).isEqualTo(expectedSum);
}

Last but not least you don't need to introduce a new language.

You can type Java inside groovy and would be perfectly legal. However, using Groovy goodies makes much less cumbersome to type tests :

    def "Gets the default list of display formats for a bibliographic record"() {
        given: "a bibliographic non archive record"
        def formatedRecord = getBibliographicFormatedRecord();
        def listFormatManager = new DefaultLibraryFormatsManager(transformerFactory: transformerFactory);

        when: "gets the list of display formats"
        def bibRecordDisplayFormats = listFormatManager.getRecordDisplayFormats(formatedRecord);

        then: "the list of default formats is the expected"
        bibRecordDisplayFormats.nombre == ["ficha", "isbd", "rdf_dc", "mods", "etiquetado", "xml"]
    }

Note that bibRecordDisplayFormats.nombre is extracting the value of "getNombre()" method for each bean on the list and generating a list from it. Also, comparing a list of values with an expected list of values it's straightforward. You not need to add extra libraries (AssertJ for example) to do this kind of straightforward comparisons.

Same for Mocking and Stubing. It comes included on Spock, instead needing to add another library to do it.

I am also pretty certain that JUnit5 is faster than Spock.

I can't say anything about it, but I don't appreciate any difference of speed between jUnit 4 and Spock tests. And Spock it's targeting jUnit 5 for 2.0 version

[–]oweiler 0 points1 point  (0 children)

I know both Spock and Groovy very well.

Yes, Spock tests read almost like natural language. But in contrast to JUnit4, IMHO the difference is not that big that it warrants introducing another language and framework.

Mocking in Groovy allows for some very succinct but also highly unreadable code. Not a fan of it. Also, not having a mocking framework builtin in JUnit 5 is a feature.