如何在Camel路由中模拟转换标记中使用的bean

时间:2015-11-26 13:05:38

标签: java spring unit-testing apache-camel mockito

我是新来的。请帮我。我正在使用Mockito。我正在寻找如何模拟转换标签的方法属性中使用的服务。我的beans-test.xml

<spring:route id="jobRoute" startupOrder="2">
  <spring:from uri="direct:start"/>
  <spring:transform>
  <spring:method ref="camelJobService" method="findByStatus"/>
  </spring:transform>
  <spring:to uri="mock:result"/>
</spring:route>

上面的服务bean总是命中数据库。测试用例失败了。我看起来很喜欢可以使用adviceWith,但我无法使用它。

然而,在我的测试中,服务被正确嘲笑。

我的测试看起来像这样

@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration(locations = "/beans-test.xml")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class CamelRouteTest extends AbstractJUnit4SpringContextTests {
@EndpointInject(uri = "mock:result")
protected MockEndpoint resultEndpoint;

@Produce(uri = "direct:start")
protected ProducerTemplate producerTemplate;

@Produce(uri = "direct:startA")
protected ProducerTemplate producerTemplate1;

@Autowired
QuartzComponent quartzComponent;

CamelJob camelJob;

@Mock
CamelJobRepository camelJobRepository;

@InjectMocks
CamelJobService camelJobService;

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
    returnCamelJob();
}

@Test
public void testJobWithStatus() throws Exception{
    when(camelJobService.findByStatus()).thenReturn(camelJob);

    resultEndpoint.expectedBodiesReceived(camelJobService.findByStatus());

    verify(camelJobService.findByStatus());
    producerTemplate.sendBody(resultEndpoint);

    resultEndpoint.assertIsSatisfied();
}

0 个答案:

没有答案