Rod*_*sta 2 mocking spring-boot junit5
我正在尝试使用 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());
}
}
Run Code Online (Sandbox Code Playgroud)
还有我的毕业
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()
}
Run Code Online (Sandbox Code Playgroud)
@AutoConfigureMockMvc 应该与@SpringBootTest或者你可以用来@WebMvcTest为有限的控制器加载 Spring MVC 基础设施。
通常,@WebMvcTest 仅限于单个控制器,并与 @MockBean 结合使用为所需的协作者提供模拟实现。
@WebMvcTest 还自动配置 MockMvc。Mock MVC 提供了一种强大的方法来快速测试 MVC 控制器,而无需启动完整的 HTTP 服务器。
您还可以通过使用@AutoConfigureMockMvc 对其进行注释,在非@WebMvcTest(例如@SpringBootTest)中自动配置MockMvc。
| 归档时间: |
|
| 查看次数: |
3440 次 |
| 最近记录: |