我正在查看Wall的Spring Boot动作手册中的一个例子。它是一个用groovy编写的简单Web应用程序。该项目正在使用Spring CLI构建,运行和测试,没有gradle构建文件,并使用Grabs.groovy文件提供H2和Thymeleaf依赖项。有两个测试类。第一个是JUnit测试,第二个是Spock规范。 JUnit测试文件是:
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))
}
}
和Spock规范是:
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))
}
}
这些文件位于项目根目录下的tests目录中。如果我使用命令&#34; spring test tests / ReadingListControllerTest.groovy&#34;运行JUnit测试。测试成功运行。如果我使用命令&#34; Spring test tests&#34;运行两个测试,则两个测试都成功运行。但是,如果我使用命令&#34; spring test tests / ReadingListControllerSpec.groovy&#34;运行Spock测试。或者通过删除ReadingListControllerTest.groovy文件并使用命令&#34; spring test tests&#34;,然后我得到以下编译错误:
ReadingListControllerSpec.groovy: 5: unable to resolve class org.mockito.Mockito
@ line 5, column 1.
import static org.mockito.Mockito.*
^
我不喜欢写Spock测试,所以我不确定问题是什么。
答案 0 :(得分:0)
我遇到了同样的问题并且无法解决这个问题,但我能够通过从ReadingListControllerSpec.groovy文件中删除导入并完全限定模拟方法而不是使用导入来解决它....
即。 org.mockito.Mockito.mock(ReadingListRepository.class)
答案 1 :(得分:0)
我遇到了同样的问题,但通过添加以下依赖项解决了这个问题。
@Grab( “org.mockito:的Mockito核:2.13.0”)
系统规范:Java 1.8.0_152 Groovy版本:2.4.13
包含依赖项后,以下命令行命令可以正常运行。
spring test tests / ReadingListControllerSpec.groovy