3

I am writing a JUnit test case for a Route class. I'm facing a problem while mocking ServiceClass inside the Processor class.

 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? 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.

user5773508
  • 91
  • 1
  • 1
  • 8

1 Answers1

-1

You are creating a mock object, however you are not injecting it anywhere (normally you are doing it with @InjectMocks annotation - read about it).

I think there are several possibilities:

  1. Provide a @MockBean object, which will be considered as a bean candidate in context.

There is a code example for mocking beans.

    @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" );
    }
    } 
  1. Mock SaveProcessor and inject it to Route class - you shouldn't take care of ServiceClient, because you are trying to test too much. Tests for SaveProcessor should be separated, tests for route don't need this logic.
staszko032
  • 802
  • 6
  • 16