我有一个带有多个 spring bean 依赖项(自动装配服务)的 spring 控制器。每个服务也很少有 spring bean 依赖项(自动装配的 daos)。比如控制器登录方法:
@Controller
@RequestMapping("/")
public class ClientAccessController extends BaseController {
@Autowired
IFileService fileService;
@Autowired
MidTierService midTierService;
/**
* Used to handle client login requests
* Works as a proxy to MID tier server
*
* @param request LoginRequest(userLogin, userPassword, compId, installGuid)
* @return LoginResponse (token)
* @throws Exception
*/
@RequestMapping(method = RequestMethod.POST, value = "/login", headers = "Content- type=application/json")
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) throws Exception {
log.info("LOGIN REQUEST [ " + request.toString() + " ]");
String token = midTierService.authenticateNativeClient(request.getLogin(), request.getPassword(), request.getGuId(),
request.getCompid().toString());
LoginResponse response = new LoginResponse(token);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
ResponseEntity<LoginResponse> responseEntity = new ResponseEntity<>(response, HttpStatus.OK);
log.info("LOGIN RESPONSE [ " + response.toString());
return responseEntity;
}
我正在尝试仅为 controller 编写 JUnit 测试ClienAccessController
。但是当我在测试类中自动装配时ClientAccessController
,spring 会尝试为所有控制器依赖项和所有嵌套依赖项(daos 等)创建 bean,但我真的不需要 em,只有MidTierService
. 那么我应该怎么做才能排除嵌套的bean初始化并只使用我在测试类中需要的东西呢?