Re: test unit a processor with an embedded autowired bean

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: test unit a processor with an embedded autowired bean

Claus Ibsen-2
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 <[hidden email]> 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 | Threaded
Open this post in threaded view
|

Re: test unit a processor with an embedded autowired bean

Damien Nicolas
Claus: I removed that lines, cause you are right, there is no need to
create bean that will be mocked
Christian: I added it, and still the same issue..

Le lun. 5 nov. 2018 à 15:10, <[hidden email]> a écrit :

> Hi Damien,
>
> I don't know exactly about Mockito's Spy capabilities, but perhaps you'd
> try it with the annotation @RunWith(MockitoJUnitRunner.class) at your class.
>
> HTH,
> Christian
>
> -----Ursprüngliche Nachricht-----
> Von: Damien Nicolas [mailto:[hidden email]]
> Gesendet: Montag, 5. November 2018 14:47
> An: [hidden email]
> Betreff: test unit a processor with an embedded autowired bean
>
>  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");
>             }
>         };
>     }
>
> }
> ----------------------------------------------------------------
> innogy SE
> Vorsitzender des Aufsichtsrates: Dr. Erhard Schipporeit
> Vorstand: Uwe Tigges (Vorsitzender), Dr. Hans Buenting,
> Dr. Bernhard Guenther, Arno Hahn, Martin Herrmann, Hildegard Mueller
> Sitz der Gesellschaft: Essen, Eingetragen beim Amtsgericht Essen,
> Handelsregister-Nr. HRB 27091, USt-IdNr. DE304171711
>


--
Damien NICOLAS