lundi 29 décembre 2014

unit testing rest service using juni,mockito

i am new to unit testing, i am trying to test a controller which gets a data from database. My issue is when i am perform get request to uri, i could not able to get the data from database.If i run the application i can able to fetch the data. but i could not do it from test code.


here is my test code:



public class RestTest {

private MockMvc mockMvc;

@Autowired
DataSource dataSource;

@Autowired
private Example exampledao;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void setUp() {

Mockito.reset(exampledao);

mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void findAllObjects() throws Exception

{
Components first = new TodoBuilder()
.cname("asdfasf")
.mdesc("asdcb")
.cdesc("asdfa")
.ccode("asdf")
.unitrateusd(24)
.build();

when(exampledao.list()).thenReturn(Arrays.asList(first));
mockMvc.perform(get("/getdata"))
.andExpect(status().isOk())


.andExpect(content().contentType(TestUtil.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(0)))
//.andExpect(jsonPath("$", hasSize(0)))
.andExpect(jsonPath("$[0].cname", is("asdfasf")))
.andExpect(jsonPath("$[0].mdesc", is("asdcb")))
.andExpect(jsonPath("$[0].cdesc", is("asdfa")))
.andExpect(jsonPath("$[0].ccode", is("asdf")))
.andExpect(jsonPath("$[0].unitrateusd", is(24)));

verify(exampledao, times(1)).list();

}


here is my actual controller which i need to check:



@RequestMapping(value="/getdata" , method=RequestMethod.GET)
public List<Components> listContact(ModelAndView model) throws IOException{
System.out.println("hii.. i made atest call");
List<Components> listContact;
listContact= exampeldao.list();


System.out.println(" but i dnt have any data.....");

return listContact;

}


I can make a call from test controller to "/getdata". But listContact=exampeldao.list() is not executing, list() is defined in some other class.but i can able to print next print statement.


when i run testclass i am getting some SecurityException:class"org.hamcrest.Matchers" signature information does not match with signature information of other class in same package.


can any let me know where i am going wrong


Aucun commentaire:

Enregistrer un commentaire