Spring Boot 1.5.2控制器层测试

时间:2017-05-05 10:37:49

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

我正在尝试使用@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&amp;找不到@SpringBootTest注释,我错过了任何库吗?我知道Spring Boot 1.5.2与Spring Boot 1.4.2相比有很多变化。

更新

现在解决了上面的问题,实际上我创建了新的测试模块和控制器在不同的模块中。我在测试模块的main-&gt; src-&gt; java下编写测试代码,并且我将spring-boot-starter-test依赖范围标记为test依赖,所以删除<scope>test</scope>,现在我可以得到@RunWith&amp; @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)

2 个答案:

答案 0 :(得分:1)

经过大量时间花在我身上后,我在webEnvironment添加了@SpringBootTest,这对我有用。

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

答案 1 :(得分:0)

@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"));
  }

}