0

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()?

  • Maybe this a dupl. of [What causes “java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean...](https://stackoverflow.com/questions/42198050/what-causes-java-lang-illegalstateexception-neither-bindingresult-nor-plain-ta)? – ErikMD Apr 11 '18 at 19:25
  • I think it has something to do with Spring data 2.0.0 findById. if(this.supplierRepository.findById(1L).isPresent()){ given(this.supplierRepository.findById(1L).get()).willReturn(supplier); } is not returning anything – Emilio Gumayagay Apr 11 '18 at 19:43

1 Answers1

0

Already solved it by changing

if(this.supplierRepository.findById((long)1).isPresent()){
        given(this.supplierRepository.findById((long)1).get()).willReturn(supplier);
    }

to

given(this.supplierRepository.findById(1L)).willReturn(Optional.of(supplier));

The problem was the first stub was returning null