2016-07-02 8 views
0

Ich versuche, eine Frühlings-Boot-1.4.0.M3 MVC Scheibe zu testen. Der Controller ist das.Frühlings-Boot 1.4 MVC-Tests mit Thymeleaf Ergebnissen in TemplateProcessingException

@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"; 
} 
} 

Die minimierte Ansicht der productshow.html thymeleaf Vorlage ist dies.

<!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> 

Und die Testklasse ist das.

@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(); 
} 
} 

Beim Ausführen des Tests wird der folgende Fehler angezeigt.

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) 

Brauchen Sie Hilfe, um dies zu beheben. Danke im Voraus.

+0

Wenn Sie es debuggen, ist der Controller tatsächlich Ihre product1 Bean bekommen? Was ist der Produktcode? Haben Sie eine vollständigere Exception-Stack-Trace? –

+0

Sie haben Recht. Der Controller erhält nicht 'product1', weil ich einen falschen Controller an '@ WebMvcTest' übergeben habe. Es sollte 'ProductController.class' anstelle von' IndexController.class' sein. Ihr Vorschlag, die Ausnahmespur im Detail zu untersuchen, hat geholfen. Vielen Dank. – user2693135

Antwort

0

Das Übergeben des richtigen Controllers an @WebMvcTest löste das Problem. Der bearbeitete Code nach dem Passieren von ProductController.class funktioniert.