Hi

I think maybe you need to use the mockito mocked instance here instead
of creating a new instance yourself

       context.bind("messageInformationService", new
MessageInformationServiceImpl());


On Mon, Nov 5, 2018 at 2:47 PM Damien Nicolas <dmn.nico...@gmail.com> wrote:
>
>  Hello,
>
> I would like to unit test a processor containing a service as an autowired
> bean (*messageInformationService*). I would like to mock this bean to make
> it return the data I want, but when I do that with Mockito, my service is
> null.
> *Here is my test class:*
>
> public class GenerateCertificateTest extends CamelTestSupport {
>
>     @Spy
>     private MessageInformationService messageInformationService;
>
>     @Before
>     public void setUp() throws Exception {
>         super.setUp();
>
>         messageInformationService =
> Mockito.mock(MessageInformationServiceImpl.class);
>         Mockito.doReturn(new
> MessageInformation()).when(messageInformationService).getMessageInformation("1",
> "2");
>     }
>
>     @Test
>     public void test1() throws Exception {
>         ObjectMapper mapper = new ObjectMapper();
>         mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
>
>         Chip chipIn = new Chip();
>         Header header = new Header();
>         header.setId("1234567890");
>         chipIn.setHeader(header);
>         Card card = new Card();
>         card.setNumber1("1");
>         card.setNNumber2("2");
>         chipIn.setCard(card);
>         Map<String, Object> headers = new HashMap<>();
>         headers.put("workflow", "test1#test2");
>
>         template.sendBodyAndHeaders("direct:start",
> mapper.writeValueAsString(chipIn), headers);
>
>         MockEndpoint mock = getMockEndpoint("mock:result");
>         mock.expectedMessageCount(1);
>         mock.message(0).body().isInstanceOf(chipIn.getClass());
>
>         Message m = mock.getExchanges().get(0).getIn();
>
>         Chip chipResult = mapper.readValue(m.getBody(String.class),
> Chip.class);
>         chipResult.setProductionStep(ProductionStep.GENERATE_CERTIFICATE);
>
>         Assert.assertEquals("test2", m.getHeader("workflow"));
>         Assert.assertEquals(chipResult.getProductionStep(),
> ProductionStep.GENERATE_CERTIFICATE);
>     }
>
>     @Override
>     protected JndiContext createJndiContext() throws Exception {
>         JndiContext context = new JndiContext();
>         context.bind("messageInformationService", new
> MessageInformationServiceImpl());
>         return context;
>     }
>
>
>     @Override
>     protected RouteBuilder createRouteBuilder() throws Exception {
>         return new RouteBuilder() {
>             @Override
>             public void configure() throws Exception {
>                 from("direct:start").process(new
> GenerateCertificate()).to("mock:result");
>             }
>         };
>     }
>
> }



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Reply via email to