Discussion:
test unit a processor with an embedded autowired bean
Damien Nicolas
2018-11-05 13:47:15 UTC
Permalink
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
2018-11-05 14:09:24 UTC
Permalink
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());
Post by Damien Nicolas
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
c***@innogy.com
2018-11-05 14:10:27 UTC
Permalink
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:***@gmail.com]
Gesendet: Montag, 5. November 2018 14:47
An: ***@camel.apache.org
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.
Damien Nicolas
2018-11-05 15:10:02 UTC
Permalink
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..
Post by c***@innogy.com
Hi Damien,
I don't know exactly about Mockito's Spy capabilities, but perhaps you'd
HTH,
Christian
-----UrsprÃŒngliche Nachricht-----
Gesendet: Montag, 5. November 2018 14:47
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
Claus Ibsen
2018-11-05 15:13:49 UTC
Permalink
Post by c***@innogy.com
Hi Damien,
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.
Post by c***@innogy.com
HTH,
Christian
-----Ursprüngliche Nachricht-----
Gesendet: Montag, 5. November 2018 14:47
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
Loading...