lundi 28 mars 2016

Running Spock specification test with spring cli

I'm looking at an example from Wall's Spring Boot in action book. It is a simple web application written in groovy. The project is being built, run and tested using Spring CLI without a gradle build file and using a Grabs.groovy file to provide H2 and Thymeleaf dependencies. There are two test classes. The first is a JUnit test and the second is a Spock specification. The JUnit tests file is:

import org.springframework.test.web.servlet.MockMvc
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
import static org.mockito.Mockito.*

class ReadingListControllerTest {

    @Test
    void shouldReturnReadingListFromRepository() {
        List<Book> expectedList = new ArrayList<Book>()
        expectedList.add(new Book(
                id: 1,
                reader: "Craig",
                isbn: "9781617292545",
                title: "Spring Boot in Action",
                author: "Craig Walls",
                description: "Spring Boot in Action is ..."
            ))

        def mockRepo = mock(ReadingListRepository.class)
        when(mockRepo.findByReader("Craig")).thenReturn(expectedList)

        def controller = new ReadingListController(readingListRepository: mockRepo)

        MockMvc mvc = standaloneSetup(controller)
                        .build()
        mvc.perform(get("/"))
            .andExpect(view().name("readingList"))
            .andExpect(model().attribute("books", expectedList))

    }

}

and the Spock specification is:

import org.springframework.test.web.servlet.MockMvc
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
import static org.mockito.Mockito.*

class ReadingListControllerSpec extends Specification {

  MockMvc mockMvc
  List<Book> expectedList

  def setup() {
    expectedList = new ArrayList<Book>()
    expectedList.add(new Book(
      id: 1,
      reader: "Craig",
      isbn: "9781617292545",
      title: "Spring Boot in Action",
      author: "Craig Walls",
      description: "Spring Boot in Action is ..."
    ))

    def mockRepo = mock(ReadingListRepository.class)
    when(mockRepo.findByReader("Craig")).thenReturn(expectedList)

    def controller = 
        new ReadingListController(readingListRepository: mockRepo)
    mockMvc = standaloneSetup(controller).build()
  }

  def "Should put list returned from repository into model"() {
    when:
      def response = mockMvc.perform(get("/"))

    then:
      response.andExpect(view().name("readingList"))
              .andExpect(model().attribute("books", expectedList))
  }

}

These files are in the tests directory off the root of the project. If I run the JUnit test with the command "spring test tests/ReadingListControllerTest.groovy" the test runs successfully. If I run both tests with the command "Spring test tests", both tests run successfully. However if I run just the Spock test either with the command "spring test tests/ReadingListControllerSpec.groovy" or by removing the ReadingListControllerTest.groovy file and using the command "spring test tests", then I get the following compile error:

ReadingListControllerSpec.groovy: 5: unable to resolve class org.mockito.Mockito
 @ line 5, column 1.
   import static org.mockito.Mockito.*
   ^

I'm not familliar with writing Spock tests so I'm not sure what the problem is.

Aucun commentaire:

Enregistrer un commentaire