Search code examples
springspring-bootspockunroll

Spring-Boot and Spock fail when using Unroll


I'm setting up a demo project with Spring-Boot. For entity-persistence, I'm using Spring generated Repository implementations based on interfaces:

@Repository
public interface MovieRepository extends JpaRepository<Movie, Long> {

    List<Movie> findByNameContaining(String name);
    List<Movie> findByRelease(LocalDate release);
    List<Movie> findByReleaseBetween(LocalDate start, LocalDate end);
    List<Movie> findByNameContainingAndRelease(String name, LocalDate release);
}

To test this, I'm using Spock with Groovy, which works wonders:

@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringBootTest
class MovieRepositoryTest extends Specification {

    @Autowired
    MovieRepository movieRepository

    @Test
    def findByNameContaining_shouldFindCorrectMovies() {
        given:
        movieRepository = this.movieRepository

        when:
        def result = movieRepository.findByNameContaining("Iron Man")

        then:
        result.size() == 3
    }
}

But as soon as I try to mix in Spock's @Unroll, everything falls apart:

@Test
@Unroll
def findByNameContaining_shouldFindCorrectMovies() {
    given:
    movieRepository = this.movieRepository

    when:
    def result = movieRepository.findByNameContaining(query)

    then:
    result.size() == expected

    where:
    query       ||  expected
    "Iron Man"  ||  3
    "Hulk"      ||  1
    "Thor"      ||  3
    "Avengers"  ||  3
    "Thanos"    ||  0
    ""          ||  20
}

Results in:

[INFO] Running com.spring.boot.demo.repositories.MovieRepositoryTest
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.003 s <<< FAILURE! - in com.spring.boot.demo.repositories.MovieRepositoryTest
[ERROR] initializationError(com.spring.boot.demo.repositories.MovieRepositoryTest)  Time elapsed: 0.003 s  <<< ERROR!
java.lang.Exception: Method $spock_feature_0_0 should have no parameters

I'm out of clues to what may cause this. Any help is welcome. Thank you

Edit 1: Well, this is interesting. I've tried the following: * Remove @Test -> java.lang.Exception: No runnable methods * Remove @RunWith and @ContextConfiguration -> Unroll works, but movieRepository is not injected / wired: java.lang.NullPointerException: Cannot invoke method findByNameContaining() on null object

Fiddling with the different annotations hasn't resulted in a working scenario though. Any guesses?


Solution

  • Yesss, I've got it:

    The RunWith was the culprit. In my Edit1, I noticed the difference with removing @Test. That got me thinking that I may be confusing JUnit testing with Spock testing. Also, the No runnable methods got me thinking. And leaving out the @RunWith since it is mostly absent in other Spock & Spring examples seemed like a good idea. And having Spring beans wired up with @ContextConfiguration is quite nice ;-). Apparently, @SpringBootTest doesn't do this?