0

I'm trying to test a Spring Boot 1.4.0.M3 MVC slice. The controller is this.

@Controller
public class ProductController {

private ProductService productService;

@Autowired
public void setProductService(ProductService productService) {
    this.productService = productService;
 }

 @RequestMapping("product/{id}")
 public String showProduct(@PathVariable Integer id, Model model){
    model.addAttribute("product", productService.getProductById(id));
    return "productshow";
 }
}

The minimized view of the productshow.html thymeleaf template is this.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
 <div class="container">  
  <h2>Product Details</h2>
    <div>
        <form class="form-horizontal">
            <div class="form-group">
                <label class="col-sm-2 control-label">Product Id:</label>
                <div class="col-sm-10">
                    <p class="form-control-static" th:text="${product.id}">Product Id</p></div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">Description:</label>
                <div class="col-sm-10">
                    <p class="form-control-static" th:text="${product.description}">description</p>
                </div>
            </div>                
        </form>
   </div>
  </div>
</body>
</html>

And the test class is this.

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ProductController.class, secure = false)
//@AutoConfigureMockMvc(secure=false)
public class ProductControllerTest {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private ProductService productServiceMock;

@Test
public void testShowProduct() throws Exception {
   Product product1 = new Product();
   product1.setId(1);
   product1.setDescription("T Shirt");
   product1.setPrice(new BigDecimal("18.95"));
   product1.setImageUrl("https://example.com/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg");
   when(productServiceMock.getProductById(1)).thenReturn(product1);

   MvcResult result = mockMvc.perform(get("/product/{id}/", 1))
           .andExpect(status().isOk())              
           .andReturn();
 }
}

On running the test, I get the following error.

2016-07-03 00:03:29.021 ERROR 6800 --- [           main]  
org.thymeleaf.TemplateEngine             : [THYMELEAF][main] Exception  
processing template "productshow": Exception evaluating SpringEL 
expression: "product.id" (productshow:19)

org.springframework.web.util.NestedServletException: Request processing  
failed; nested exception is 
org.thymeleaf.exceptions.TemplateProcessingException: Exception 
evaluating SpringEL expression: "product.id" (productshow:19)

Need help to resolve this. Thanks in advance.

user2693135
  • 1,206
  • 5
  • 21
  • 44
  • If you debug it, is the controller actually getting your product1 bean? What is the code of Product? Do you have a more complee exception stack trace? – JB Nizet Jul 02 '16 at 22:32
  • You are right. The controller isn't getting `product1` because I was passing a wrong controller to `@WebMvcTest`. It should have been `ProductController.class` instead of `IndexController.class`. Your suggestion of examining the exception trace in detail helped. Thanks. – user2693135 Jul 03 '16 at 04:57

1 Answers1

0

Passing the correct controller to @WebMvcTest resolved the issue. The edited code after passing ProductController.class is working.

user2693135
  • 1,206
  • 5
  • 21
  • 44