I'm trying to mock rest api call but facing an error while testing the controller class using 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
I have an project structure something like this,
I tried many ways but no luck, Below is my restcontroller and its Mockito test class and repository,
@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());
}
Does my project structure is making any problem? I know it is not able to find the definition but not getting why it is so. Any suggestion would be nice.