ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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๋กœ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ ์•ˆ๋จนํ˜”๋˜๊ฑธ ๊ฐ•์ œ๋กœ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

    ๊ทธ ๊ฒฐ๊ณผ ์ด๋ ‡๊ฒŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ์ž˜ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

Designed by Tistory.