Getting an issue while testing Rest API

10 views
Skip to first unread message

Aashish Mishra

unread,
Apr 7, 2021, 9:55:47 AM4/7/21
to mockito
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'museum.repository.UsersRepository' in your configuration.

this is my repository

@Repository
public interface UsersRepository extends CrudRepository<users,String> {
@Query(value="select * from users where username =:userName",nativeQuery=true)
users findOne(@Param("userName") String userName);

@Query(value="select a.authority from users u,authorities a where a.username =:userName and u.username=a.username",nativeQuery=true)
String applyJoin(@Param("userName") String userName);
}

this is the testing code 

@RunWith(SpringRunner.class)
@WebMvcTest(TestController.class)
public class TestControllerMvcTest {

@Autowired
private MockMvc mvc;

@Autowired
private ObjectMapper objectMapper;

@Test
public void getAllUsersTest() throws Exception {
List<users> userList=new ArrayList<>();
userList.add(new users("ashish","pass","Ashish","KumarMishra",true));
userList.add(new users("denzel","pass","denzel","washington",true));
userList.add(new users("gagan","pass","Gagan","shrivastava",true));
userList.add(new users("kunj","pass","Kunj","Shah",true));
userList.add(new users("newArtist","pass","astist","kumar",true));


String url="/getTheUserList";
MvcResult result=mvc.perform(get(url)).andExpect(status().isOk()).andReturn();
String actualResponse=result.getResponse().getContentAsString();
System.out.println("hi"+result.getResponse().getContentAsString());

String expectedResponse=objectMapper.writeValueAsString(userList);
System.out.println(expectedResponse);
assertEquals(actualResponse,expectedResponse);
}
}

------------------------------------------------
this is the controller

@RestController
public class TestController {


@Autowired
UsersRepository usersRepo;

@Autowired
private ObjectMapper objectMapper;

@GetMapping("/getTheUserList")
public List<users> getListOfUsers() throws JsonProcessingException {
return (List<users>) usersRepo.findAll();

}
}
Reply all
Reply to author
Forward
0 new messages