git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: test unit a processor with an embedded autowired bean


On Mon, Nov 5, 2018 at 3:10 PM <christian.jacob@xxxxxxxxxx> wrote:
>
> 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.
>

Yeah if you use that @Spy annotation then you may need some kind of
mockitio runner to kick-in and initialize that field based on that
@Spy annotation.
Then you may look at not extending CamelTestSupport and use those
jrunit runner annotaitons and whatelse you need to set on top of the
class.

Try to find som mockit @Spy examples with junit and then go from
there. Or find another way to do the @Spy stuff with maybe the mockit
java api to set it up yourself.

> HTH,
> Christian
>
> -----Ursprüngliche Nachricht-----
> Von: Damien Nicolas [mailto:dmn.nicolas@xxxxxxxxx]
> Gesendet: Montag, 5. November 2018 14:47
> An: users@xxxxxxxxxxxxxxxx
> 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



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