This is my controller class
@Controller
public class MySpringController {
@Resource(name="myLogic")
private MyService myService;
@RequestMapping(value = "/employees", method = RequestMethod.GET)
public String getPersons(Model model) {
List<MyHibernateJPA> employeesEspn = myService.getAll();
model.addAttribute("employeesEspn", employeesEspn);
return "myFormPage";
}
@RequestMapping(value = "/employees/add", method = RequestMethod.GET)
public String add(ModelMap model){
MyHibernateJPA espn = new MyHibernateJPA();
model.addAttribute("espn", espn);
return "addemployee";
}
@RequestMapping(value = "/employees/add", method = RequestMethod.POST)
public String addPost(ModelMap model, @ModelAttribute("espn") MyHibernateJPA passedValues){
myService.add(passedValues.getSname(), passedValues.getScourse());
return "redirect:/employees";
}
}
This is my service class
package org.test.service;
@Service("myLogic")
@Transactional
public class MyService {
@Autowired
private SessionFactory sessionFactory;
public List<MyHibernateJPA> getAll() {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM MyHibernateJPA");
return query.list();
}
public void add(String sname, String scourse) {
Session session = sessionFactory.getCurrentSession();
MyHibernateJPA employee = new MyHibernateJPA();
employee.setSname(sname);
employee.setScourse(scourse);
session.save(employee);
}
}
In my model class i have just created entities. I am trying to write the unit test for my controller
@Test
public void getAll_ShouldAddTodoEntriesToModelAndRenderTodoListView() throws Exception {
MyHibernateJPA first = new MyHibernateJPA()
.sno(1)
.sname("Lorem ipsum")
.scourse("Foo");
MyHibernateJPA second = new MyHibernateJPA()
.sno(2L)
.sname("Lorem ipsum")
.scourse("Bar")
.build();
when(todoServiceMock.getAll()).thenReturn(Arrays.asList(first, second));
However I think that this is not the correct way as i am seeing errors. How to pass the value for sno, sname and scourse so that i can check it?
Aucun commentaire:
Enregistrer un commentaire