In my application, I am fetching customer details from an API and saving this customer to the database. after saving this customer object to in my database, I am returning customer object with id generated by the database. this is my rest Controller layer for getting customer object from API.
//add a new customer and then return all details of newly created customer
@PostMapping("/customer")
public Customer addCustomer(@RequestBody Customer theCustomer)
{
// also just in case they pass an id in JSON ... set id to 0
// this is to force a save of new item ... instead of update
theCustomer.setId(0);
return theCustomerService.saveCustomer(theCustomer);
}
this is my service layer
@Service
public class CustomerServiceImpl implements CustomerService {
private CustomerDAO theCustomerDAO;
// set up constructor injection
@Autowired
public CustomerServiceImpl(CustomerDAO theCustomerDAO)
{
this.theCustomerDAO=theCustomerDAO;
}
@Override
@Transactional
public Customer saveCustomer(Customer thCustomer) {
return theCustomerDAO.saveCustomer(thCustomer);
}
}
and this my CustomerDAO layer where I am saving it to database
public Customer saveCustomer(Customer theCustomer)
{
// get the current hibernate session
Session currentSession = entityManager.unwrap(Session.class);
//save the customer
currentSession.saveOrUpdate(theCustomer);
return theCustomer;
}
above parts of my Application are working properly but now I want to add testing in it. so I created a test method for the service layer.
class CustomerServiceImplTest {
@Test
void saveCustomer() {
CustomerDAO theCustomerDAO=mock(CustomerDAO.class);
CustomerServiceImpl theCustomerServiceImpl=new CustomerServiceImpl(theCustomerDAO);
Customer inCustomer=new Customer("john","nick","google@gmail.com","CPOI939","8607574640");
inCustomer.setId(0);
Customer outCustomer=inCustomer;
outCustomer.setId(9);
when(theCustomerDAO.saveCustomer(inCustomer)).thenReturn(outCustomer);
assertEquals(outCustomer,theCustomerServiceImpl.saveCustomer(inCustomer));
}
}
But I am not sure that it's a good way of testing because we are not adding any business logic in the service layer. so how do I test it and which layer should be tested.
Try to test this case on the integration level. There is no business logic just pure crud to save data to DB.
You can use DbUnit
and in-memory databases like H2
.
DbUnit is a JUnit extension targeted at database-driven projects that, among other things, puts your database into a known state between test runs.
example test:
@Test
@DatabaseSetup("sampleData.xml")
public void testSaveCustomer() throws Exception {
Customer inCustomer=new Customer("john","nick","google@gmail.com","CPOI939","8607574640");
theCustomerServiceImpl.saveCustomer(inCustomer)
List<Customer> customerList = customerService.findAll();
assertEquals(1, customerList.size());
assertEquals("john", customerList.get(0).getName());
...
}
More details Spring nad DBUnit
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.