Well, actually i dont use unit test. I test it manually. Here are my codes :
CustomerController
Code:
@Controller
@RequestMapping(value = "/customer")
public class CustomerTransactionController {
@Autowired
private CustomerServiceImpl customerDAO;
private final Logger logger = LoggerFactory
.getLogger(CustomerTransactionController.class);
@RequestMapping(method = RequestMethod.GET)
public String getCustomer(Model model) {
List<Customer> customers = customerDAO.findAll();
model.addAttribute("customers", customers);
return "customerHomePage";
}
@RequestMapping(value = "/addCustomer", method = RequestMethod.GET)
public String add(@ModelAttribute Customer customer) {
return "addCustomer";
}
@RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
public String addSuccess(@ModelAttribute Customer customer) {
customerDAO.save(customer);
logger.info("redirecting to customer home page...");
return "redirect:/customer";
}
}
customerServiceImpl
Code:
@Repository("customerDAO")
@Transactional
public class CustomerServiceImpl implements CustomerService {
@PersistenceContext
private EntityManager em;
private final Logger logger = LoggerFactory
.getLogger(CustomerServiceImpl.class);
public void save(Customer customer) {
logger.info("Inserting new customer");
em.merge(customer);
logger.info("Inserting new customer finsihed");
}
@SuppressWarnings("unchecked")
public List<Customer> findAll() {
logger.info("searching all customers...");
Query query = this.em.createQuery("from Customer");
return query.getResultList();
}
}
customer
Code:
@Entity
@Table(name = "Customer")
public class Customer {
@Id
@GeneratedValue
private Long id;
@Column(name = "Firstname")
private String firstName;
@Column(name = "Lastname")
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
and this is the log after the execution.
Code:
INFO : com.rodel.SpringApp.HomeController - Welcome home!
INFO : com.rodel.SpringApp.CustomerServiceImpl - searching all customers...
Hibernate: select customer0_.id as id0_, customer0_.firstname as firstname0_, customer0_.lastname as lastname0_ from customer customer0_
INFO : com.rodel.SpringApp.CustomerServiceImpl - Inserting new customer
INFO : com.rodel.SpringApp.CustomerServiceImpl - Inserting new customer finsihed
INFO : com.rodel.SpringApp.CustomerTransactionController - redirecting to customer home page...
INFO : com.rodel.SpringApp.CustomerServiceImpl - searching all customers...
Hibernate: select customer0_.id as id0_, customer0_.firstname as firstname0_, customer0_.lastname as lastname0_ from customer customer0_
hope it helps to solve the problem.
thanks.