Spring 将一个MockBean注入另一个MockBean
我有一个典型的Spring应用程序,我正试图通过MockMvc进行测试。该应用程序包含一些数据库调用和一些第三方api调用,我想在测试端到端流时模拟所有这些调用,第三方除外 这就是我所创造的- 控制器类Spring 将一个MockBean注入另一个MockBean,spring,mocking,junit5,Spring,Mocking,Junit5,我有一个典型的Spring应用程序,我正试图通过MockMvc进行测试。该应用程序包含一些数据库调用和一些第三方api调用,我想在测试端到端流时模拟所有这些调用,第三方除外 这就是我所创造的- 控制器类 public class PortfolioController { private final PortfolioService portfolioService; } 服务等级 public class PortfolioService { private final Portfoli
public class PortfolioController {
private final PortfolioService portfolioService;
}
服务等级
public class PortfolioService {
private final PortfolioTransactionRepository portfolioTransactionRepository;
private final AlphavantageService alphavantageService;
}
AlphaVantage服务
public class AlphavantageService {
private ApiConfig apiConfig;
private final RestTemplate restTemplate;
public Map<String, List<Candle>> getStockQuotes(List<String> symbols) {
return symbols.stream().collect(Collectors.toMap(symbol -> symbol, symbol -> getQuotes(symbol)));
}
}
公共级AlphavantageService{
私人ApiConfig ApiConfig;
私有最终RestTemplate RestTemplate;
公共地图getStockQuotes(列表符号){
返回symbols.stream().collect(Collectors.toMap(symbol->symbol,symbol->getQuotes(symbol));
}
}
现在是考验-
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = PortfolioController.class)
class PortfolioControllerTest {
private List<PortfolioTransaction> transactions;
@MockBean
private AlphavantageService alphavantageService;
@MockBean
private PortfolioService portfolioService;
@Autowired
private PortfolioController portfolioController;
@Autowired
private MockMvc mockMvc;
}
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers=PortfolioController.class)
类PortfolioControllerTest{
私人清单交易;
@蚕豆
私人AlphavantageService AlphavantageService;
@蚕豆
私人PortfolioService PortfolioService;
@自动连线
专用PortfolioController PortfolioController;
@自动连线
私有MockMvc-MockMvc;
}
问题是,当我尝试在服务器上执行任何mvc调用时,AlphaVantageService
不会被注入到PortfolioService
中,所以在第1级之前,我会注入bean,但在进一步的级别上,我不会得到相同的结果
是故意的还是我遗漏了什么?我们应该如何测试这样的测试用例?实际上,在尝试了一些选项之后,我找到了一个解决方案 就像
@MockBean
,spring也有一个叫做@SpyBean
的概念。这解决了我的问题。现在我的测试如下所示
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = PortfolioController.class)
@MockBeans(value = {@MockBean(AlphavantageService.class),
@MockBean(PortfolioTransactionRepository.class)})
@SpyBeans(value = {@SpyBean(PortfolioService.class)})
class PortfolioControllerTest {
private List<PortfolioTransaction> transactions;
@Autowired
private AlphavantageService alphavantageService;
@Autowired
@SpyBean
private PortfolioService portfolioService;
@Autowired
private PortfolioController portfolioController;
@Autowired
private MockMvc mockMvc;
}
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers=PortfolioController.class)
@MockBeans(值={@MockBean(AlphavantageService.class)),
@MockBean(PortfolioTransactionRepository.class)})
@SpyBeans(值={@SpyBean(PortfolioService.class)})
类PortfolioControllerTest{
私人清单交易;
@自动连线
私人AlphavantageService AlphavantageService;
@自动连线
@间谍
私人PortfolioService PortfolioService;
@自动连线
专用PortfolioController PortfolioController;
@自动连线
私有MockMvc-MockMvc;
}
这很有魅力,我可以在测试中使用成熟的依赖注入。实际上,在尝试了一些选项之后,我找到了一个解决方案 就像
@MockBean
,spring也有一个叫做@SpyBean
的概念。这解决了我的问题。现在我的测试如下所示
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = PortfolioController.class)
@MockBeans(value = {@MockBean(AlphavantageService.class),
@MockBean(PortfolioTransactionRepository.class)})
@SpyBeans(value = {@SpyBean(PortfolioService.class)})
class PortfolioControllerTest {
private List<PortfolioTransaction> transactions;
@Autowired
private AlphavantageService alphavantageService;
@Autowired
@SpyBean
private PortfolioService portfolioService;
@Autowired
private PortfolioController portfolioController;
@Autowired
private MockMvc mockMvc;
}
@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers=PortfolioController.class)
@MockBeans(值={@MockBean(AlphavantageService.class)),
@MockBean(PortfolioTransactionRepository.class)})
@SpyBeans(值={@SpyBean(PortfolioService.class)})
类PortfolioControllerTest{
私人清单交易;
@自动连线
私人AlphavantageService AlphavantageService;
@自动连线
@间谍
私人PortfolioService PortfolioService;
@自动连线
专用PortfolioController PortfolioController;
@自动连线
私有MockMvc-MockMvc;
}
这很有魅力,我可以在测试中使用成熟的依赖注入