1

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?

BramP
  • 51
  • 6
  • 1
    Are you sure you need to put @Test on top of the method? I thought Spock didn't require that jUnit annotation... – Hans Westerbeek Oct 19 '18 at 15:42
  • 1
    I agree with @HansWesterbee, you don't need to use `@Test`. Not sure if it's the cause of an error though. Most likely `@RunWith(SpringRunner.class)` and `@ContextConfiguration` are obsolete as well. The `@SpringBootTest` annotation should be enough to start the context. Please remove them and try again. If it doesn't help what Spring Boot and Spock version are you using? – puczo Oct 19 '18 at 15:54
  • 2
    I mentioned it because the annotation could confuse the Groovy magic that Spock does. – Hans Westerbeek Oct 19 '18 at 17:24
  • That's worth a try. Will do that ASAP. Tnx – BramP Oct 20 '18 at 16:37

1 Answers1

1

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?

BramP
  • 51
  • 6