2017-05-09 4 views
1

부팅 통합 단위 테스트 NoSuchBeanDefinitionException이 예외 : I는 단위를 쓸 때봄 아래와 같이 내가 샘플 IntegrationFlow을 만들어

RegisterHostFlow.RegisterHostGateway registerHostGateway = applicationContext.getBean(RegisterHostFlow.RegisterHostGateway.class); 
    Host host1 = registerHostGateway.registerHost(host); 

:

@Configuration 
@EnableIntegration 
@IntegrationComponentScan 
@ComponentScan 
public class RegisterHostFlow { 
    private final Logger LOG = LoggerFactory.getLogger(this.getClass()); 

    @MessagingGateway 
    public interface RegisterHostGateway{ 
     @Gateway(requestChannel = "registerHostInputChannel") 
     Host registerHost(Host host); 
    } 

    @Bean 
    public IntegrationFlow httpInboundGatewayFlow() { 
     return IntegrationFlows.from("registerHostInputChannel") 
      .handle((host, headers) -> { 
         return host; 
      }) 
      .enrich(e -> e 
        .requestPayload(Message::getPayload) 
        .property("uuid", "34563456345634563456") 
        .property("id", "1234") 
      ) 
      .get(); 
    } 
} 

나는 다음과 같이 스프링 MVC 컨트롤러에서이 호출하고 아래에 보여지는 것처럼 위생 테스트를하기위한 테스트, 어플리케이션이 에러와 함께로드되지 않음, NoSuchBeanException :

@RunWith(SpringRunner.class) 
@WebMvcTest(HostController.class) 
@EnableIntegration 
@IntegrationComponentScan 
public class HostControllerTest { 
@Autowired 
private MockMvc mvc; 

@Test 
public void registerHost_passedInHost_returnJson() throws Exception { 
    this.mvc.perform(post("/hostservice/v1/hosts").contentType(MediaType.APPLICATION_JSON). content('someJsonStringGoesHere')) 
      .andExpect(status().isOk()) 
      .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)); 
} 
012516410 다음은 617,451,515,

예외는 내가 볼 수 있습니다 : ApplicationContext를 때 테스트 모드에서 통합 콩을 autowire하기가하는 방법에

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'registerHostInputChannel' available 

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) 
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167) 
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134) 
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134) 
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134) 
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) 
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134) 
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155) 

모든 포인터?

답변

2

따르면 @WebMvcTest JavaDoc을 :

* Typically {@code @WebMvcTest} is used in combination with {@link MockBean @MockBean} or 
* {@link Import @Import} to create any collaborators required by your {@code @Controller} 
* beans. 
당신은 같은 테스트 클래스 설정을해야

:

@RunWith(SpringRunner.class) 
@WebMvcTest(HostController.class) 
@Import(RegisterHostFlow.class) 
public class HostControllerTest { 

그래서,이 방법 당신은 봄 통합의 얼굴에 MVC 슬라이스와 협력자를 가지고 목표 흐름 구성.