我在运行我的
EmployeeControllerTest
类时遇到以下错误。我被指示不要修改控制器类。有人可以帮我解决这个问题吗?
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“...controllers.EmployeeController”的bean时出错:通过字段“testService1”表达的依赖关系不满足:没有可用的“...services.TestService1”类型的合格bean:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我尝试了以下方法:
使用反射设置私有字段, 测试类中的构造函数注入, 创建一个测试配置类来添加必要的 bean, 手动设置依赖项, 我仍然无法解决这个问题。
@ContextConfiguration(classes = {TestConfiguration.class})
@WebFluxTest(controllers = EmployeeController.class)
@Profile("local")
public class EmployeeControllerTest {
@MockBean
private TestService1 testService1;
@MockBean
private TestService2 testService2;
private WebTestClient webTestClient; /* If I use @Autowired for this field, I'm getting an UnsatisfiedDependencyException: Error creating bean due to an unsatisfied dependency expressed through the field 'webTestClient'.*/
@InjectMocks
private EmployeeController employeeController;
private JwtAuthenticationToken jwtAuthenticationToken;
int pageIndex=0;
int pageSize=10;
@BeforeEach
void setup(){
MockitoAnnotations.openMocks(this);
jwtAuthenticationToken = mock(JwtAuthenticationToken.class);
when(jwtAuthenticationToken.getName()).thenReturn("testUser");
/*If I don't use the code below, I get the error: Cannot invoke "org.springframework.test.web.reactive.server.WebTestClient.post()" because "this.webTestClient" is null.*/
webTestClient = WebTestClient.bindToController(employeeController).webFilter(new SecurityContextServerWebExchangeWebFilter()).build();
}
@Test
void testEmployee(){
Page<EmployeeDto> mockPage = new PageImpl<>(Collections.emptyList());
when(testService1.list(eq(jwtAuthenticationToken), eq(pageIndex), eq(pageSize))).thenReturn(Mono.just(mockPage));
webTestClient.post() .uri("/test/employee/{index}/{size}",pageIndex,pageSize).exchange().expectStatus().isOk().expectBodyList(EmployeeDto.class).hasSize(0);
}}
/*Controller Code:I've been instructed not to modify the controller class*/
@RestController
@RequestMapping("/test")
public class EmployeeController {
@Autowired //Field injection is not recommended
private TestService1 testService1;
@Autowired //Field injection is not recommended
private TestService2 testService2;
@PostMapping("/employee/{index}/{size}")
public Mono<Page<EmployeeDto>> users(@NonNull JwtAuthenticationToken principal, @PathVariable int index, @PathVariable int size) {
return testService1.list(principal, index, size);
}
}
测试运行成功,如下。
@ExtendWith(MockitoExtension.class)
添加到测试类的注释中。@Autowired
添加到 WebTestClient 的声明中private EmployeeController employeeController;
。它是通过 WebFluxTest 处理的。以下测试的完整设置,省略测试用例:
@ContextConfiguration(classes = {TestConfiguration.class})
@WebFluxTest(controllers = EmployeeController.class)
@ExtendWith(MockitoExtension.class)
public class EmployeeControllerTest {
@MockBean
private TestService1 testService1;
@MockBean
private TestService2 testService2;
@Autowired
private WebTestClient webTestClient;
private JwtAuthenticationToken jwtAuthenticationToken;
int pageIndex=0;
int pageSize=10;
@BeforeEach
void setup(){
MockitoAnnotations.openMocks(this);
jwtAuthenticationToken = mock(JwtAuthenticationToken.class);
when(jwtAuthenticationToken.getName()).thenReturn("testUser");
//removed this code:
// webTestClient = WebTestClient.bindToController(employeeController)
// .webFilter(new SecurityContextServerWebExchangeWebFilter()).build();
}
//your test methods follow...
}