I'm trying to test my controller that takes the form for updating supplier
//get supplier form for update
@GetMapping("/{id}")
public String getSupplierUpdateForm(@PathVariable Long id, Model model) {
if(supplierRepository.findById(id).isPresent()){
model.addAttribute("supplier",supplierRepository.findById(id).get());
}
return "supplier";
}
so far I was able to write this test
@Test
public void testGetSupplierUpdateForm()throws Exception{
Supplier supplier = new Supplier();
supplier.setId((long)1);
supplier.setSupplierName("ABC Company");
supplier.setAddress("Bayombong, Nueva Vizcaya");
supplier.setContactNumber("N/A");
if(this.supplierRepository.findById((long)1).isPresent()){
given(this.supplierRepository.findById((long)1).get()).willReturn(supplier);
}
mvc.perform(get("/supplier/1"))
.andExpect(status().isOk())
.andExpect(view().name("supplier"))
.andExpect(model().attributeExists("supplier"))
.andExpect(model().attribute("supplier", equalTo(supplier)));
}
but when I run it I get this error
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "supplier" - line 16, col 25)
Line 16 from my view looks like this
<input type="hidden" th:field="*{id}" />
I also get errors from these kinds of lines
<title th:text="${#strings.isEmpty(supplier.id) ? 'New Supplier' : 'Update Supplier '+supplier.id }"></title>
I think it has something to do with the supplier entity. Am I doing this test right? What can I do to fix these problems?
After Investigating a bit I found that
if(this.supplierRepository.findById((long)1).isPresent()){
given(this.supplierRepository.findById((long)1).get()).willReturn(supplier);
}
is not returning doing anything since
this.supplierRepository.findById((long)1).isPresent()
is false.
How can I make use of findById in a given()?