【问题标题】:Add REST controller/route to SpringBoot test将 REST 控制器/路由添加到 SpringBoot 测试
【发布时间】:2019-05-30 11:04:02
【问题描述】:

是否可以向 SpringBoot 测试添加路由/端点(我不想将其包含在源代码中,而是将其留在测试中)?

@RestController
class HelloAPI {
    @GetMapping("/hello")
    public String ok() {
        return "world";
    }
}

更新: 事实证明不需要额外的配置 - HelloAPI 类应该从 src/main 移动到 src/test。而已。但是,它对所有@SpringBoot 测试都是“可见的”。

所以问题是:如何将这个 bean (HelloAPI) 的创建(在 ApplicationContext 中注册)限制为特定的测试类?

See link to an example project

【问题讨论】:

    标签: spring spring-boot spring-boot-test


    【解决方案1】:

    您可以为此使用@TestConfiguration

    @ExtendWith(SpringExtension.class)
    @SpringBootTest(webEnvironment = DEFINED_PORT)
    class EmbeddedApiTest {
    
        @Test
        void testSomething() {
            ...
        }
    
        @TestConfiguration
        public static class TestCfg {
    
            @RestController
            @RequestMapping("/test")
            public class TestApi {
    
                @GetMapping("/hello")
                public String hello() {
                    return "Hello World!";
                }
            }
        }
     }
    

    【讨论】:

      【解决方案2】:

      您可以添加一个静态的 inner @Configuration 类来设置 bean:

      @RunWith(SpringRunner.class)
      ... // Other test annotations
      public class YourTestClass {
      
          @Configuration
          static class ContextConfiguration {
              @Bean
              public HelloAPI helloAPI() {
                  return new HelloAPI();
              }
          }
      
          @Test
          public void someTest_shouldXxx() { ... }
      }
      

      并确保将您的 HelloAPI 类添加到 /src/test 而不是 /src/main/

      但是请注意,(test)ApplicationContext 将在类中的所有测试都已执行后被销毁,从而也销毁了HelloAPI bean。

      如果您正在寻找向RestTemplate 提供测试端点的方法,请将MockRestServiceServer 绑定到您的RestTemplate instead

      【讨论】:

      • 如果我将 HelloAPI 类放入/src/test,它将被组件扫描automatically 拾取(由于@RestController 注释),因此既不需要静态@Configuration,也不需要任何@TestConfiguration。
      【解决方案3】:

      虽然我已经接受了 Anatoliy 的回答(他正在使用 Java 和 JUnit5),但我想分享我的 Kotlin 代码(使用旧的 JUnit4):

      @RunWith(SpringRunner::class)
      @SpringBootTest(webEnvironment = RANDOM_PORT)
      class TestWithInnerConfigTest {
          @LocalServerPort var port: Int? = null
          @Autowired private lateinit var restTemplate: TestRestTemplate
      
          @Test
          fun testingMainEndpoint() {
              val url = "http://localhost:$port/helloworld"
              val resp = restTemplate.exchange(url, GET, null, String::class.java)
              assertEquals(resp.statusCode, OK)
          }
      
          @Test
          fun testingTestEndpoint() {
              val url = "http://localhost:$port/test"
              val resp = restTemplate.exchange(url, GET, null, String::class.java)
              assertEquals(resp.statusCode, NOT_FOUND)
          }
      
          companion object {
              @TestConfiguration
              class TestCfg {
                  @RestController
                  class TestApi {
                      @GetMapping("/test") fun hello() = "Helloworld!"
                  }
              }
          }
      }
      

      使用静态内部@TestConfiguration 类的好处是它只应用于这个文件。这是link to the complete example

      【讨论】:

        猜你喜欢
        • 2015-12-22
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-08
        • 1970-01-01
        相关资源
        最近更新 更多