mardi 30 décembre 2014

unit testing using junit,mokito


i am new to unit testing, and i stuck while testing a spring mvc controller using junit,mockito which gets a data from data base. Here is my spring mvc controller code:



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


System.out.println("hiii i made a call but i dnt have any data.....");

return listContact;

}


i can able to make a call from test code to "/getdata" uri, but i could not able to execute listContact= exampeldao.list(); statement. i have defined list() in some other class.


here is my test code:



public class RestTest {
@InjectMocks
ComponentController contrller;
@Mock
Example exampleda;

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("$[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)));
}


and when i run unit test i am getting a SecurityException:org.hamcrest.Matchers signature information does not match wit signature of other classes in same package...


any one help me to sort out this issue


Thanks





Aucun commentaire:

Enregistrer un commentaire