我试图使用MockMvc,但它总是显示以下错误:
原因: org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有可用的类型为'org.springframework.test.web.servlet.MockMvc'的限定bean:预计至少有1个bean符合自动装配候选条件。依赖注释: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我的类:
@AutoConfigureMockMvc
@ExtendWith(SpringExtension.class)
@TestPropertySource(locations = "classpath:application-cliente.properties")
public class ClienteRepositoryTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ClienteService clienteService;
@Test
public void simpleMockTest() throws Exception {
var clienteMock = new Cliente("Jessica Pereira");
Mockito.doReturn(Optional.of(clienteMock)).when(clienteService).buscar(2L);
this.mockMvc.perform(MockMvcRequestBuilders.get("/api/clientes/pf/{id}", 1L))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
我的gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.rjdesenvolvimento'
version = '0.0.1'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
ext {
set('springCloudVersion', 'Greenwich.SR1')
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-oauth2'
implementation 'org.springframework.cloud:spring-cloud-starter-security'
compileOnly 'org.projectlombok:lombok'
compile('org.glassfish.jaxb:jaxb-runtime:2.3.1')
runtimeOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'junit', module: 'junit'
} '
testRuntime 'org.junit.jupiter:junit-jupiter-api'
testRuntime 'org.junit.jupiter:junit-jupiter-engine'
testRuntime 'com.h2database:h2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
test {
useJUnitPlatform()
}