Everything seems to be right in my test but I keep getting this error on the book service call to find all
i have mocked the class i am using correctly .
#Test
#WithMockUser(username = "admin", roles={"ADMIN"})
public void bookRemoveTest() throws Exception {
Book book = new Book();
List<Book> expectedBookList = createBookList(10);
/* expect */ bookService.removeOne(anyLong());
EasyMock.expectLastCall();
replay(bookService);
bookService.removeOne(anyLong());
// Assert.assertEquals("expectedBookList", bookService.findAll());
expect(bookService.findAll()).andReturn(expectedBookList);
mockMvc
.perform(post("/book/remove")
.accept(MediaType.TEXT_HTML)
.contentType(MediaType.TEXT_HTML)
.param("id","12345678" ))
.andExpect(model().attributeExists("bookList"))
.andExpect(view().name("bookList"))
.andExpect(content().contentType(MediaType.TEXT_HTML))
.andReturn();
}
You should call replay method after you finished the setup.
So please move the expect(bookService.findAll()) call before the replay(bookService) and it should be good.
Related
I have mocked several dependencies and defined their behavior. I do not wish for real methods to be called on my dependencies - the whole point of mocking.
My test:
#Test
public void should_Create_MultiDoc_Ticket() throws URISyntaxException{
DataTransformationService transformationService = Mockito.mock(DataTransformationService.class);
JSONObject samplePolicyData = createSamplePolicyData("Sample Multidoc Template");
InteractiveRequest sampleInteractiveRequest = createSampleInteractiveRequest();
when(transformationService.transformMultiDocData(createSampleInteractiveData())).thenReturn(createSampleInteractiveData());
String ticketId = interactiveTicketService.createTicket(samplePolicyData.toString());
assertThat(ticketId).isEqualTo("sampleTicketId");
verify(transformationService, times(1)).transformMultiDocData(createSampleInteractiveData());
}
Specifically, the real transformationService.transformMultiDocData() method is being called, when it shouldn't be.
The error when I run the test:
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://us-data-transformation-sample/template": us-data-transformation-sample; nested exception is java.net.UnknownHostException: us-data-transformation-sample
at com.lmig.us.services.DataTransformationService.transformMultiDocData(DataTransformationService.java:114)
at com.lmig.us.services.InteractiveTicketService.createTicket(InteractiveTicketService.java:56)
at com.lmig.us.services.InteractiveTicketServiceTest$createTicket.should_Create_MultiDoc_Ticket(InteractiveTicketServiceTest.java:311)
... 82 more
Does anyone know what's going on here?
You should write the test like this:
#ExtendWith(MockitoExtension.class)
public class InteractiveTicketServiceTest {
#Mock
DataTransformationService transformationService;
#InjectMocks
InteractiveTicketService interactiveTicketService;
#Test
public void should_Create_MultiDoc_Ticket() throws URISyntaxException{
JSONObject samplePolicyData = createSamplePolicyData("Sample Multidoc Template");
InteractiveRequest sampleInteractiveRequest = createSampleInteractiveRequest();
when(transformationService
.transformMultiDocData(createSampleInteractiveData()))
.thenReturn(createSampleInteractiveData());
String ticketId = interactiveTicketService.createTicket(samplePolicyData.toString());
assertThat(ticketId).isEqualTo("sampleTicketId");
verify(transformationService, times(1)).transformMultiDocData(createSampleInteractiveData());
}
}
I'm building a microservice using Spring Boot. I wrote an API with GET-, POST-, PUT-, DELETE- Methods, run the application and tested it using Postman - everything's working fine...
But testing the PUT-Method fails with
java.lang.AssertionError: Status expected:<204> but was:<400>
Running the test in debug-mode and stepping throw throws an InvocationTargetException:
My RestController-Methods looks like this:
#PutMapping(value = "/{id}")
public ResponseEntity updateSongById(#PathVariable("id") Integer id, #RequestBody #Validated
SongDto songDto) {
// TODO Add authorization
SongDto song = songService.getSongById(id);
if (song == null)
return new ResponseEntity(HttpStatus.BAD_REQUEST);
return new ResponseEntity(songService.updateSong(id, songDto), HttpStatus.NO_CONTENT);
}
songService.getSongById(id):
#Override
public SongDto getSongById(Integer id) {
return songMapper.songToSongDto(songRepository.findById(id)
.orElseThrow(NotFoundException::new));
}
The SongRepository is just a simple Interface which extends JpaRepository<Song, Integer>.
My failing test looks like this:
#Test
void updateSongById_success() throws Exception {
when(songService.updateSong(anyInt(), any(SongDto.class))).thenReturn(getValidSongDto());
String songDtoJson = objectMapper.writeValueAsString(getValidSongDto());
mockMvc.perform(put("/rest/v1/songs/1")
.content(songDtoJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
And getValidSongDto() just provides a Dto used in my tests:
private SongDto getValidSongDto() {
return SongDto.builder()
.id(1)
.title("TestSongValid")
.label("TestLabelValid")
.genre("TestGenreValid")
.artist("TestArtistValid")
.released(1000)
.build();
}
I really don't understand at the moment, what I did wrong to make this test fail and also couldn't find anything in the internet which helped me solving this problem, so far. So, therefor I'd be very thankful, if anybody could tell me what's wrong here and how to solve this issue.
Thank you very much!!
You need to return the value for songService.getSongById as shown below
#Test
void updateSongById_success() throws Exception {
when(songService.getSongById(Mockito.any())).thenReturn(getValidSongDto());
when(songService.updateSong(anyInt(), any(SongDto.class))).thenReturn(getValidSongDto());
String songDtoJson = objectMapper.writeValueAsString(getValidSongDto());
mockMvc.perform(put("/rest/v1/songs/1")
.content(songDtoJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent());
}
Frustrating. Everywhere i look, i see samples of testing async Vertx code, but nothing that comes close to what i am trying to test.
Vertx 3.3.2, JUnit 4.12, Java 8
The method under test sends a message to the event bus. I want to verify that what happens in the eventBus().send() response handler is happening.
Sooooooo many examples i see have the eventBus().send() method in the TEST ITSELF (thus, testing the other end of the event bus - the consumer) I want to test the response handler in the .send()
I have tried Async in the test. Tried ArgumentCaptor. Tried Thread.sleep(). Tried doAnswer(). Nothing seems to get the test to (a) wait for the async eventBus().send() call in the method under test to finish and (b) able to verify() that there was an interaction (i think this might have to do with the different between the Vertx.TestContext and the JUnit.Runner Context..)
Code:
Method under test:
public void sendToEventBusAddress(RoutingContext context, String requestId, String userId) {
List<String> stuff = split(context.request().getParam("stuffToSplit"));
JsonObject eventBusMessage = new JsonObject()
.put("requestId", requestId)
.put("stuffList", new JsonArray(stuff))
.put("userId", userId);
LOGGER.info("Putting message: {} onto the EventBus at address: {}", eventBusMessage.encodePrettily(), EventBusEndpointEnum.STUFF_ACCESS.getValue());
context.vertx().eventBus().send(EventBusEndpointEnum.STUFF_ACCESS.getValue(), eventBusMessage, new DeliveryOptions().setSendTimeout(timeout), async -> {
if (async.succeeded()) {
LOGGER.info("EventBus Response: {}", async.result().body().toString());
context.response().setStatusCode(HttpStatus.SC_OK);
context.response().headers().set(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
context.response().end(async.result().body().toString());
} else {
LOGGER.error(errorMessage);
context.response().setStatusCode(HttpStatus.SC_BAD_REQUEST);
context.response().end(errorMessage);
}
});
}
Simplified (non-working) Test case and class:
#RunWith(VertxUnitRunner.class)
public class MyBrokenTest {
#Mock private RoutingContext routingContext;
#Mock private HttpServerRequest contextRequest;
#Mock private HttpServerResponse contextResponse;
#Mock private MultiMap responseHeaders;
#Rule public RunTestOnContext rule = new RunTestOnContext();
#Before
public void setUp(TestContext context) {
MockitoAnnotations.initMocks(this);
}
#Test
public void testOne(TestContext context) {
when(routingContext.vertx()).thenReturn(rule.vertx());
when(routingContext.request()).thenReturn(contextRequest);
when(contextRequest.getParam("stuffToSplit")).thenReturn("04MA");
when(routingContext.response()).thenReturn(contextResponse);
when(contextResponse.headers()).thenReturn(responseHeaders);
rule.vertx().eventBus().consumer(EventBusEndpointEnum.STUFF_ACCESS.getValue(), res -> {
res.reply("yo");
});
ClassUnderTest cut= new ClassUnderTest(180000);
cut.sendToEventBusAddress(routingContext, "testRequestId", "UnitTestId");
verify(contextResponse).setStatusCode(200);
}
}
I know that the test in its current form won't work, because the method under test returns as soon as the eventBus().send() method is called inside the method, and therefore, the verify fails with 'no interactions'.
What i can't figure out, is how to verify it properly given the async nature of Vertx!
Thanks
I did it so:
At #BeforeAll annotated method I deploy only the sending verticle.
At #BeforeEach - I create a consumer for the given message and store message(s) to variable/collection
Something like:
receivedMessage = new Message[1];
eventBus.consumer("DB",
message -> {
message.reply("OK");
receivedMessage[0] = message;
});
context.completeNow();
In test I validate stored value(s):
client.get(8080, "localhost", "/user/" + id)
.as(BodyCodec.string())
.send(context.succeeding((response -> context.verify(() -> {
Assertions.assertEquals(expectedMessage, receivedMessage[0].body());
context.completeNow();
}))));
when I was sync I wrote unit tests mocking the persistence part and check the caller's behavior. Here is an example about what I usually did:
#Mock
private OfferPersistenceServiceImpl persistenceService;
#Inject
#InjectMocks
private OfferServiceImpl offerService;
...
#Test
public void createInvalidOffer() {
offer = new Offer(null, null, null, null, null, 4, 200D, 90D);
String expectedMessage = Offer.class.getName() + " is not valid: " + offer.toString();
Mockito.when(persistenceService.create(offer)).thenThrow(new IllegalArgumentException(expectedMessage));
Response response = offerService.create(offer);
Mockito.verify(persistenceService, Mockito.times(1)).create(offer);
Assert.assertEquals(INVALID_INPUT, response.getStatus());
String actualMessage = response.getEntity().toString();
Assert.assertEquals(expectedMessage, actualMessage);
}
But now I fell in love with Vertx.io (to which I am pretty new) and I want to be async. Nice. But Vertx has handlers, so the new persistence component to mock looks like this:
...
mongoClient.insert(COLLECTION, offer, h-> {
...
});
So I am guessing how to mock handler h to tests class who's using that mongoClient or even if it is the right way to test with Vertx.io. I am using vertx.io 3.5.0, junit 4.12 and mockito 2.13.0. Thanks.
Update
I tried to follow tsegimond suggestion but I can't get how Mockito's Answer and ArgumentCaptor can help me. Here is what I tried so far.
Using ArgumentCaptor:
JsonObject offer = Mockito.mock(JsonObject.class);
Mockito.when(msg.body()).thenReturn(offer);
Mockito.doNothing().when(offerMongo).validate(offer);
RuntimeException rex = new RuntimeException("some message");
...
ArgumentCaptor<Handler<AsyncResult<String>>> handlerCaptor =
ArgumentCaptor.forClass(Handler.class);
ArgumentCaptor<AsyncResult<String>> asyncResultCaptor =
ArgumentCaptor.forClass(AsyncResult.class);
offerMongo.create(msg);
Mockito.verify(mongoClient,
Mockito.times(1)).insert(Mockito.anyString(), Mockito.any(), handlerCaptor.capture());
Mockito.verify(handlerCaptor.getValue(),
Mockito.times(1)).handle(asyncResultCaptor.capture());
Mockito.when(asyncResultCaptor.getValue().succeeded()).thenReturn(false);
Mockito.when(asyncResultCaptor.getValue().cause()).thenReturn(rex);
Assert.assertEquals(Json.encode(rex), msg.body().encode());
and using Answer:
ArgumentCaptor<AsyncResult<String>> handlerCaptor =
ArgumentCaptor.forClass(AsyncResult.class);
AsyncResult<String> result = Mockito.mock(AsyncResult.class);
Mockito.when(result.succeeded()).thenReturn(true);
Mockito.when(result.cause()).thenReturn(rex);
Mockito.doAnswer(new Answer<MongoClient>() {
#Override
public MongoClient answer(InvocationOnMock invocation) throws Throwable {
((Handler<AsyncResult<String>>)
invocation.getArguments()[2]).handle(handlerCaptor.capture());
return null;
}
}).when(mongoClient).insert(Mockito.anyString(), Mockito.any(),
Mockito.any());
userMongo.create(msg);
Assert.assertEquals(Json.encode(rex), msg.body().encode());
And now I got confused. Is there a way to mock an AsyncResult to let it return false on succeed()?
Finally I got some times to investigate and I made it. Here is my solution.
#RunWith(PowerMockRunner.class)
#PowerMockRunnerDelegate(VertxUnitRunner.class)
#PrepareForTest({ MongoClient.class })
public class PersistenceTest {
private MongoClient mongo;
private Vertx vertx;
#Before
public void initSingleTest(TestContext ctx) throws Exception {
vertx = Vertx.vertx();
mongo = Mockito.mock(MongoClient.class);
PowerMockito.mockStatic(MongoClient.class);
PowerMockito.when(MongoClient.createShared(Mockito.any(), Mockito.any())).thenReturn(mongo);
vertx.deployVerticle(Persistence.class, new DeploymentOptions(), ctx.asyncAssertSuccess());
}
#SuppressWarnings("unchecked")
#Test
public void loadSomeDocs(TestContext ctx) {
Doc expected = new Doc();
expected.setName("report");
expected.setPreview("loremipsum");
Message<JsonObject> msg = Mockito.mock(Message.class);
Mockito.when(msg.body()).thenReturn(JsonObject.mapFrom(expected));
JsonObject result = new JsonObject().put("name", "report").put("preview", "loremipsum");
AsyncResult<JsonObject> asyncResult = Mockito.mock(AsyncResult.class);
Mockito.when(asyncResult.succeeded()).thenReturn(true);
Mockito.when(asyncResult.result()).thenReturn(result);
Mockito.doAnswer(new Answer<AsyncResult<JsonObject>>() {
#Override
public AsyncResult<JsonObject> answer(InvocationOnMock arg0) throws Throwable {
((Handler<AsyncResult<JsonObject>>) arg0.getArgument(3)).handle(asyncResult);
return null;
}
}).when(mongo).findOne(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
Async async = ctx.async();
vertx.eventBus().send("persistence", new JsonObject(), msgh -> {
if (msgh.failed()) {
System.out.println(msgh.cause().getMessage());
}
ctx.assertTrue(msgh.succeeded());
ctx.assertEquals(expected, Json.decodeValue(msgh.result().body().toString(), Doc.class));
async.complete();
});
async.await();
}
}
Use Powemockito to mock the MongoClient.createShared static method, so you'll have your mock when verticle starts. Mocking async handler is a bit of code to write. As you can see mocking start at Message<JsonObject> msg = Mockito.mock(Message.class); and ends at Mockito.doAnswer(new Answer.... In the Answer's method pick the handler param and force it to handle your async result then you're done.
Normally, I'd use a comment to post this, but formatting gets lost. The accepted solution is works great, just note that it can be simplified a bit using Java 8+, and you can use your actual objects instead of JSON.
doAnswer((Answer<AsyncResult<List<Sample>>>) arguments -> {
((Handler<AsyncResult<List<Sample>>>) arguments.getArgument(1)).handle(asyncResult);
return null;
}).when(sampleService).findSamplesBySampleFilter(any(), any());
getArgument(1), refers to the index of the handler argument in a method such as:
#Fluent
#Nonnull
SampleService findSamplesBySampleFilter(#Nonnull final SampleFilter sampleFilter,
#Nonnull final Handler<AsyncResult<List<Sample>>> resultHandler);
I have a function that processes events obtained by a PullSubscription to Microsoft Exchange.
public void processEvents(ExchangeService service, PullSubscription subscription)
throws Exception {
GetEventsResults events = subscription.getEvents();
// Loop through all item-related events.
for (ItemEvent itemEvent : events.getItemEvents()) {
if (itemEvent.getEventType() == EventType.NewMail) {
EmailMessage message = EmailMessage.bind(service, itemEvent.getItemId());
EmailParser emailParser = new EmailParser();
emailParser.parse(message, service);
}
}
}
I am trying to test it using PowerMockito because ExchangeService is a final class.
So I have mocked ExchangeService and PullSubscription as follows:
ExchangeService serviceMock = PowerMockito.mock(ExchangeService.class);
PullSubscription subscriptionMock = PowerMockito.mock(PullSubscription.class);
#Test
public void startPullNotification() throws Exception {
ProcessEmails pr = new ProcessEmails("config.properties");
pr.startPullNotification(serviceMock);
}
When I'm trying to test it using the following code it throws a NullPointerException because subscription.getEvents() returns null (i.e. the subscriptionMock has no events in it).
I tried stubbing it by mocking the eventResults that has to be returned:
when(subscriptionMock.getEvents()).thenReturn(eventResultsMock);
It doesn't work since the getEvents() is not called in the test function. I wanted to know how to test this function?
http://archive.msdn.microsoft.com/ewsjavaapi
Solution:
I had to mock every object being created in the function.
Also, I had to add the following above the class declaration.
#RunWith(PowerMockRunner.class)
#PrepareForTest({ ClassBeingTested.class })