1
我正在編寫一個測試,其中我想測試其餘控制器。 我嘲笑這是在控制,但春天也希望這是裏面的服務庫中的服務...由於NoSuchBeanDefinitionException而無法測試彈簧休息控制器
@RunWith(SpringRunner.class)
@WebMvcTest(AccountRestController.class)
public class AccountRestControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private PremiumAccountService service;
AccountRequestResource resource = new AccountRequestResource(25d);
PremiumAccount premiumAccount = new PremiumAccount("abc", "cba", LocalDateTime.now(), 25d);
@Test
public void testGetAccountWithAvailableGB() throws Exception {
given(service.getAccountByGB(25d)).willReturn(Optional.of(premiumAccount));
mockMvc.perform(post("/api/account"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.login", is("abc")));
}
}
堆棧:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'premiumaccountservice.repository.PremiumAccountRepository' available: expected at least 1 bean which qualifies as autowire candidate.
AccountRestController:
@RestController
@RequestMapping("/api")
public class AccountRestController {
private final PremiumAccountService service;
@Autowired
public AccountRestController(PremiumAccountService service) {
this.service= service;
}
}
看起來像一個公開的問題。 [github.com/spring-projects/spring-boot/issues/6663] –
您可以發佈完整的stacktrace和'AccountRestController'的代碼嗎?您是否可能將'PremiumAccountRepository'注入到您的'AccountRestController'中? 您使用哪個春季版本? – fap
@fap不,在'AccountRestController'中'PremiumAccountService'只被注入。我正在使用彈簧引導1.5.6.RELEASE –