我正在尝试模拟rest api调用但在使用WebMvcTest测试控制器类时遇到错误,
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.abc.center.entities.repositories.SomeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 41 more
我有一个像这样的项目结构,
我尝试了很多方法,但没有运气,下面是我的restcontroller及其Mockito测试类和存储库,
@Slf4j
@Component
@RequestMapping()
@Setter
public class SomeController {
// Variable initialization
@Autowired
private SometRepository someRepository;
public void sendData(RecordNo rocordNo, String xmlString, SomeFile file) throws ValidationException{
ClientHttpRequestFactory requestFactory = new
HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
RestTemplate restTemplate = new RestTemplate(requestFactory);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
restTemplate.setMessageConverters(messageConverters);
MultiValueMap<String,String> header = new LinkedMultiValueMap<>();
header.add("x-api-key",api_key);
header.add("Content-Type",content_type);
header.add("Cache-Control",cache_control);
HttpEntity<String> request = new HttpEntity<>(xmlString, header);
try {
restTemplate.postForEntity(getUri(rocordNo,file), request, String.class);
}catch (RestClientResponseException e){
throw new ValidationException(e.getResponseBodyAsString());
}
}
getUri(RecordNo rocordNo SomeFile file){
// here I need someRepository which is an interface
}
}
public interface TestRepository extends PagingAndSortingRepository<RecordNo, Integer> {
//queries to repositories
}
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(SomeController.class)
public class SomeControllerTestClass {
private TestController serviceToTest;
private String xmlString = "String";
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
@Mock
private TestRepository testRepository;
@Before
public void init(){
serviceToTest.setTestRepository(testRepository);
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
MockitoAnnotations.initMocks(this);
}
@Test
public void verifySafe2Call() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.post("someuri")
.contentType(MediaType.APPLICATION_XML)
.accept(MediaType.APPLICATION_JSON)
.content(xmlString)).andExpect(jsonPath("$.responseMessage").value("Validation succeeded"))
.andExpect(jsonPath("$.responseCode").value("SUCCESS"))
.andDo(print());
}
我的项目结构是否有任何问题?我知道它无法找到定义,但却没有找到原因。 任何建议都会很好。
答案 0 :(得分:0)
试试这个, 将以下代码添加到控制器:
@Autowired
private SometRepository someRepository;
public void setRepository(SomeRepository repository) {
this.repository = repository;}
答案 1 :(得分:0)
我没有看到@EnableJpaRepositories
注释。
您的配置应使用此批注进行批注,Spring将扫描所需的软件包包。
@Configuration
@EnableJpaRepositories(basePackages = {"com.abc.center.entities.repositories"})
public class JpaConfig {
}
答案 2 :(得分:0)
我在使用@WebMvcTest
编写单元/集成测试时遇到了类似的问题,我通过将@ComponentScan("com.abc.center.entities.repositories")
添加到SomeController
(在您的情况下)解决了这个问题。