-
[Error - SpringBoot] mustache ํ๊ธ ๊นจ์ง ํ์๐ปProgramming/Error 2023. 10. 3. 21:26
๐ ์ค๋ฅ ๋ฐ์ ์ํฉ
mustache์ ํ๊ธ์ ์จ๋๊ณ , ํ ์คํธ ์ฝ๋๋ฅผ ๋๋ ธ๋๋ฐ assertThat ๋ฉ์๋์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
<!DOCTYPE HTML> <html> <head> <title>์คํ๋ง ๋ถํธ ์น ์๋น์ค</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <h1>์คํ๋ง ๋ถํธ๋ก ์์ํ๋ ์น ์๋น์ค</h1> </body> </html>
์ด๋ฐ ์ฝ๋์๋๋ฐ
package com.sujin.book.springboot.web; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class IndexControllerTest { @Autowired private TestRestTemplate restTemplate; @Test public void ๋ฉ์ธํ์ด์ง_๋ก๋ฉ() { // URL ํธ์ถ ์ ํ์ด์ง์ ๋ด์ฉ์ด ์ ๋๋ก ํธ์ถ๋๋์ง์ ๋ํ ํ ์คํธ // when String body = this.restTemplate.getForObject("/", String.class); // then assertThat(body).contains("์คํ๋ง ๋ถํธ๋ก ์์ํ๋ ์น ์๋น์ค"); } }
์ด ํ ์คํธ ์ฝ๋์์ ์ค๋ฅ๊ฐ ๋ ๊ฒ์ด๋ค. "์คํ๋ง ๋ถํธ๋ก ์์ํ๋ ์น ์๋น์ค"๋ผ๋ ๋ฌธ์์ด์ด ์๋์ง๋ง ๋น๊ตํ๋ ํ ์คํธ ์ฝ๋์ด๋ค.
java.lang.AssertionError: Expecting actual: "<!DOCTYPE HTML> <html> <head> <title>??? ?? ? ???</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <h1>??? ??? ???? ? ???</h1> </body> </html>" to contain: "์คํ๋ง ๋ถํธ๋ก ์์ํ๋ ์น ์๋น์ค" at com.sujin.book.springboot.web.IndexControllerTest.๋ฉ์ธํ์ด์ง_๋ก๋ฉ(IndexControllerTest.java:25) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:578) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:76) at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
mustache์ ํ๊ทธ๋ค ์์ ๊ธ์๋ค์ด "???" ์ด๋ฐ์์ผ๋ก ๋จ๋ ๊ฒ์ ๋ณด๋ ๋น์ฐํ ์ ๋ฌธ์์ด๊ณผ ์ผ์นํ์ง ์์์ ํ ์คํธ์ฝ๋๋ฅผ ํต๊ณผํ์ง ๋ชปํ ๊ฒ ๊ฐ๋ค.
๐ ํด๊ฒฐ ๋ฐฉ๋ฒ
application.properties ํ์ผ์
server.servlet.encoding.force-response=true
์ด ์ฝ๋๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค. ์ด ์ฝ๋๋ HTTP ์๋ต์ ๋ฌธ์ ์ธ์ฝ๋ฉ์ ๊ฐ์ ๋ก ์ค์ ํ๋ ์ต์ ์ด๋ค. true๋ก ์ค์ ํ๋ฉด Spring Boot ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ HTTP ์๋ต์ ์บ๋ฆญํฐ ์ธ์ฝ๋ฉ์ ์ง์ ํ ์บ๋ฆญํฐ์ ์ผ๋ก ๊ฐ์ ํ๋ค๋ ๊ฒ์ด๋ค. ์์ mustache ์ฝ๋์์ charset์ UTF-8๋ก ํด์ฃผ์๋๋ฐ ์๋จนํ๋๊ฑธ ๊ฐ์ ๋ก ํด์ฃผ๋ ๊ฒ์ด๋ค.
๊ทธ ๊ฒฐ๊ณผ ์ด๋ ๊ฒ ํ ์คํธ ์ฝ๋๊ฐ ์ ๋์๊ฐ๋ ๊ฒ์ ๋ณผ ์ ์๋ค!
'๐ปProgramming > Error' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ