我使用的是Spring Boot 1.5.7。 我试图在@SpringBootTest中使用@SpyBean,但我仍然得到NoUniqueBeanDefinitionException。
@MockBean适用于@SpringBootTest,@ SpyBean正在发生 这是我们测试的代码:
@RunWith (SpringRunner.class)
@SpringBootTest
public class CustomerServiceSpyTest {
@Autowired
private CustomerService customerService;
@MockBean (name = "customerOrderRepository")
private CustomerOrderRepository customerOrderRepository;
@SpyBean (name = "httpSession")
private HttpSession httpSession;
@Test
public void saveMyOrderProductCountInSession () throws Exception {
// given
Customer customer = new Customer ();
given (httpSession.getAttribute ("loginUser"))
.willReturn (customer);
CustomerOrder order = new CustomerOrder ();
order.addProduct (new Product ());
order.addProduct (new Product ());
given (customerOrderRepository.findAllByCustomer (customer))
.willReturn (Stream.of (order));
// when
customerService.saveMyOrderProductCountInSession ();
int count = (Integer) httpSession.getAttribute ("orderCount");
// then
assertThat (count, is (2));
}
}