S
S
SankaSanka2021-01-17 19:39:00
Java
SankaSanka, 2021-01-17 19:39:00

How to make thymeleaf friends with an integration test?

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"/>
  <title th:text="${title}"> </title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<header th:insert="blocks/header :: header"></header>
 <div th:each="el : ${product}" class="allert allert-info mt-2">  
<div class="card" style="width: 18rem;" id="showCase-list">
  <div class="card-body">
    <h2 class="card-title"><p th:text="${el.name}" ></h2>
    <p class="card-text"><p th:text="${el.description}" ></p>
   <p class="card-text"><p th:text="${el.price}" ></p>
     <p><a th:href="'/product/'+${el.id}" class="btn btn-primary btn-sm">View Details </a></p><br>
  </div>
</div>
</div>
<div th:insert="blocks/footer :: footer"></div>
</body>
</html>


tested like this

@Test
  public void createShowcaseTest() throws Exception {
    this.mockMvc.perform(get("/showcase"))
        .andDo(print())
        .andExpect(xpath("//div[@id='showCase-list']").nodeCount(4));
  }


everything goes well but does not accept my html
org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 14; Attribute name "xmlns:" associated with an element type "html" must be followed by the ' = ' character.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:262)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at org.springframework.test.util.XpathExpectationsHelper.parseXmlByteArray(XpathExpectationsHelper.java:237)
at org.springframework.test.util.XpathExpectationsHelper.evaluateXpath(XpathExpectationsHelper.java:219)

at org.springframework.test.util.XpathExpectationsHelper.assertNodeCount(XpathExpectationsHelper.java:154)
.......

Please tell me how to fix this.
Simply inserting "=" as it asks does not work there.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BorLaze, 2021-01-17
@BorLaze

In the Thymeleaf documentation, the code is:

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

Try it - suddenly it will help.

S
SankaSanka, 2021-01-18
@SankaSanka

decision

@Autowired
  private WebApplicationContext wac;

  @Before
  public void setup() {
    MockitoAnnotations.initMocks(this);
    this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question