When is handleTransportError() method is thrown in stompClient of spring boot. If it occurs in afterConnected method, will the rest of code continue to work or it returns from the method?
Related
I'm new to Java programming and I have the following snippet on which I want to write unit test:
Response response = request.get();
if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
return response.readEntity(type);
}
I'm able to create the scenario where HTTP request returns a valid response using the below code:
stubFor(get("someUrl").willReturn(aResponse().withStatus(200)));
I want to create another scenario where the method call response.readEntity(type) throws an exception. For this, I require that request.get() method returns me a mocked object so that I can define the desired behavior on the mocked object.
I read the documentation provided at http://wiremock.org/docs to find how to do this behavior but didn't find any way to return a mocked object as HTTP response.
Also, the request variable is not injected and hence I can't mock it directly.
You cannot do something like
stubFor(get("/$metadata?annotations=true").willReturn(aResponse().withStatus(200).withBody(Mock()));. It is because wiremock acts only as http server mock. Only thing you can configure is response (ex. in JSON).
What you can do is to return for example 400 and error code body from wiremock and check if you code accepts this message and act on it correctly.
I am writing some contract tests and I am trying to mock my controller in order to test the wanted method. My method should only return status code 200, so not an object, and I do not know how to write this with Mono or Flux and I get an error because of that.
I tried something like this, but it does not work:
Mono<Integer> response = Mono.just(Response.SC_OK);
when(orchestration.paymentReceived(purchase)).thenReturn(response);
How should I write my "when" part in order to verify it returns status code 200?
In order to check response status code you will need to write a more complicated test, using WebTestClient. Like so:
Service service = Mockito.mock(Service.class);
WebTestClient client = WebTestClient.bindToController(new TestController(service)).build();
Now you are able to test:
serialization to JSON or other types
content type
response code
path to your method
invoked method (POST,GET,DELETE, etc)
Unit tests do not cover above topics.
// init mocks
when(service.getPersons(anyInt())).thenReturn(Mono.just(person));
// execute rest resource
client.get() // invoked method
.uri("/persons/1") // requested path
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk() // response code
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.firstName").isEqualTo(person.getFirstName())
.jsonPath("$.lastName").isEqualTo(person.getLastName())
// verify you have called your expected methods
verify(service).getPerson(1);
You can find more examples here. Above test is also does not require Spring context, can work with mock services.
I should call one Rest call with some json payload after this it should invoke one more end point with the same payload (POST) call and it should give some response. I should capture that response .
How can I do that using Jax rs and Spring?
I have a mixed Scala/Java Play! application (version 2.5.11). I just moved the WebSocket handling from Java to Scala but the controller receiving the request and returning the response is still in Java. Now I can't access cookies anymore (nor any Http.context)
In more detail:
The Java controller receives the request for a WebSocket and calls the Scala service.
The Scala service produces a play.api.mvc.WebSocket that is then given to the Java controller which more or less just gives it to the client.
Usually WebSockets in Java are of type play.mvc.WebSocket.
Somehow if I use the Scala WebSocket Play! doesn't give me the Http.context: If I call Http.Context.current().request().cookies() I just get a
java.lang.RuntimeException: There is no HTTP Context available from here.
Does anyone know how I can access Http.Context in an controller action that returns a play.api.mvc.WebSocket in Java?
Instead of relying to the java Http.Context class, have you considered just to use the request object that you can pass to the controller method?
The controller method will look like this:
def socket = WebSocket.acceptOrResult[String, String] { request =>
// headers
val headers = request.headers
// cookies
val cookies = request.cookies
// your websocket process
// ...
}
I have process belows :
Service1,Service2,Service3 call method a() of DAO together.
How to find service calling in inside method a()?