【问题标题】:Spring Boot 1.5.2 Controller Layer TestingSpring Boot 1.5.2 控制器层测试
【发布时间】:2017-10-03 19:26:15
【问题描述】:

我正在尝试使用@RunWith@SpringBootTest 测试我的控制器。

控制器

@RestController
public class HomeController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String get(HttpServletResponse response) throws IOException {
        return "Hello World";
    }
}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class SampleTestNGApplicationTests{

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testHome() throws Exception {

        ResponseEntity<String> entity = this.restTemplate.getForEntity("/home", String.class);

        assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(entity.getBody()).isEqualTo("Hello World");
    }

}

用于测试的依赖项

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
</dependency>

现在找不到@RunWith@SpringBootTest 注释,我是否缺少任何库?我知道 Spring Boot 1.5.2 与 Spring Boot 1.4.2 相比有很多变化。

更新

上述问题现已解决,实际上我创建了新的测试模块,控制器位于不同的模块中。我在测试模块的main->src->java下写测试代码,我把spring-boot-starter-test的依赖范围标记为test,所以去掉&lt;scope&gt;test&lt;/scope&gt;,现在可以得到@RunWith&@SpringBootTest注释。

现在我收到错误 @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)

错误日志

=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:
-----------------

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - @ConditionalOnWebApplication (required) found 'session' scope (OnWebApplicationCondition)

   DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
      - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)

【问题讨论】:

  • jar 损坏,导入错误。很难说。

标签: java spring spring-boot junit spring-test


【解决方案1】:

在花了很多时间之后,我在@SpringBootTest 上添加了webEnvironment,它对我有用。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

【讨论】:

    【解决方案2】:
    @SpringBootTest(classes = Application.class)
    

    将加载整个应用程序上下文。我想如果你只想测试你的控制器层,那是没有必要的。你可以这样做

    @RunWith(SpringRunner.class)
    @WebMvcTest(controllers = HomeController.class)
    public class HomeControllerTest {
    
      @Autowired
      private MockMvc mvc;
    
      @MockBean
      private SomeService someservice // Mock any other dependencies which you have in your controller.  
    
      @Test
      public void someTest() throws Exception {
    
        doAnswer(invocation -> {
          // return some predefined data which would be returned from your service layer
        }).when(someservice).someMethod();
    
        mvc.perform(MockMvcRequestBuilders.get("/home").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string("expectedString"));
      }
    
    }
    

    【讨论】:

    • 我删除了 SpringBootTest 并添加了 WebMvcTest,但出现异常“无法加载 ApplicationContext”
    • 删除 @WebAppConfiguration 并且不要使用 RestTemplate(这需要设置整个应用程序上下文)使用 Mockmvc,如我给出的示例中所示
    猜你喜欢
    • 2018-01-02
    • 2018-04-27
    • 2018-03-25
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2017-12-03
    相关资源
    最近更新 更多