[英]Apache camel Junit mock issue
I am writing a JUnit test case for a Route
class. 我正在为
Route
类编写一个JUnit测试用例。 I'm facing a problem while mocking ServiceClass
inside the Processor
class. 我在
Processor
类中模拟ServiceClass
遇到了问题。
public class SaveRouteTest extends CamelTestSupport {
private Exchange exchange;
protected ProducerTemplate template;
private SaveRequestBuilder saveRequestBuilder;
private SaveRoute route;
private SaveProcessor saveProcessor;
private ApplicationContext springContext = createApplicationContext();
@Mock
SaveServiceClient saveServiceClient;//Not able to mock this class
@BeforeClass
public void prepareTestCamelContext() throws Exception {
route = springContext.getBean("saveRoute", saveRoute.class);
saveProcessor = springContext.getBean("saveProcessor",
SaveProcessor.class);
saveRequestBuilder = springContext.getBean("saveRequestBuilder",
SaveRequestBuilder.class);
}
@BeforeMethod
public void init() throws SQLException, ServiceException {
MockitoAnnotations.initMocks(this);
exchange = new DefaultExchange(context);
}
@Override
protected RouteBuilder[] createRouteBuilders() throws Exception {
template = context.createProducerTemplate();
return new RouteBuilder[]{route};
}
@Test
public void testHotelCommitTransactionRouteSuccessReturn() throws
Exception {
when(saveServiceClient.invokeServiceWithName(anyObject()).
thenReturn("Response");
exchange.getIn().setBody("Request detail");
exchange = template.send("direct:SaveRoute",exchange);
}
protected ApplicationContext createApplicationContext() {
return new ClassPathXmlApplicationContext("classpath*:config/spring/testContext.xml");
}
}
@Component
public class SaveRoute extends SPRouteBuilder {
@Autowired
private SaveProcessor saveProcessor;
@Override
public void configure() throws Exception {
from("direct:SaveRoute")
.routeId("save")
.to("direct:ProcessSaveFlow")
.end();
from("direct:ProcessSaveFlow")
.process(saveProcessor)
.end();
}
}
public class SaveProcessor implements Processor {
@Autowired
SaveServiceClient saveServiceClient;
@Override
public void process(Exchange exchange) throws Exception {
//This line of code not able to mock
String response = saveServiceClient.invokeServiceWithName(exchange);
exchange.getIn().setBody(response);
}
}
How to resolve mocking of saveServiceClient.invokeServiceWithName
? 如何解决
saveServiceClient.invokeServiceWithName
? The debugger is always going inside this method. 调试器总是在这个方法里面。 I tried using both mock objects and an injected mock.
我尝试使用模拟对象和注入模拟。 I can't make the method call directly.
我无法直接调用方法。
You are creating a mock object, however you are not injecting it anywhere (normally you are doing it with @InjectMocks annotation - read about it). 您正在创建一个模拟对象,但是您没有在任何地方注入它(通常您使用@InjectMocks注释进行注释 - 阅读它)。
I think there are several possibilities: 我认为有几种可能性:
There is a code example for mocking beans. 有一个模拟bean的代码示例。
@RunWith ( CamelSpringRunner.class )
@SpringBootTest
public class RouteBuilderTest extends CamelSpringTestSupport {
@Autowired
private ApplicationContext applicationContext;
@MockBean
private ServiceClient serviceClient;
@Override
public void setUp() throws Exception {
MockitoAnnotations.initMocks( this );
super.setUp();
}
@Override
public void tearDown() {
}
@Test
public void test() {
when( serviceClient.doStuff() ).thenReturn( "mockedResponse" );
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.