Я пытаюсь проверить свой контроллер с помощью MicronautTest. Но я получаю следующую ошибку:
2020-04-23 03:33:22,649 [nioEventLoopGroup-6-2] TRACE io.micronaut.http.client.DefaultHttpClient - {
"message" : "Content Type [text/json] not allowed. Allowed types: [application/json]",
"_links" : {
"self" : {
"href" : "/digital-sky/pilots/frn",
"templated" : false
}
}
}
2020-04-23 03:33:22,649 [nioEventLoopGroup-6-2] TRACE io.micronaut.http.client.DefaultHttpClient - ----
io.micronaut.http.client.exceptions.HttpClientResponseException: Content Type [text/json] not allowed. Allowed types: [application/json]
at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1821)
at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1739)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
Я вижу, что ошибка говорит о том, что она ожидает применения - json. Но я не знаю, как решить эту проблему. Я прошел некоторые уроки по тестам Micronaut, но они смогли вызывать методы клиент / контроллер без настройки типов носителей в модульных тестах. Я новичок в Miconaut, и я не уверен, чего здесь не хватает. Кто-нибудь может помочь? Спасибо.
Это файлы,
PilotController. java
@Controller(BASE_URL)
public class PilotController implements PilotOperations, BaseOperations {
@Inject
private PilotTransactionService pilotTransactionService;
@Override
public Single<HttpResponse<UUID>> createPilotV1(@Nullable UUID correlationId, String source, String token, CreatePilotRequest createPilotRequest) {
return pilotTransactionService.create(correlationId, source, token, createPilotRequest);
}
}
PilotOperations. java
import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.version.annotation.Version;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
import io.micronaut.http.annotation.Body;
import io.micronaut.validation.Validated;
//Some other imports...
@Validated
@Introspected(classes = {CreatePilotRequest.class, PersonalIdentityDocumentType.class, PilotTrainingData.class,
PilotTrainingDataLight.class, PilotTrainingDocumentType.class})
public interface PilotOperations {
String BASE_URL = "/digital-sky";
@Version("1")
@Post("/pilots/frn")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_JSON)
Single<HttpResponse<UUID>> createPilotV1(@Nullable @Header UUID correlationId,
@Header String source,
@Header("x-auth") String token,
@Valid @Body CreatePilotRequest createPilotRequest);
}
PilotClient. java и PilotTestClient. java для Junit
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.client.annotation.Client;
//Some other imports here...
@Client(BASE_URL)
public interface PilotClient extends PilotOperations {
Single<HttpResponse<UUID>> createPilotV1(@Nullable UUID correlationId, @Header String source, String token, CreatePilotRequest createPilotRequest);
}
PilotControllerTest. java
@MicronautTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class PilotControllerTest implements PersonTestDataProvider {
@Inject
private PilotTestClient pilotTestClient;
@Inject
private PersonClient personClient;
@MockBean(PersonClient.class)
PersonClient personClient() {
return mock(PersonClient.class);
}
@Order(1)
@Test
void testCreatePilot() {
final Person person = getMSPersonTestData();
final CreatePilotRequest pilot = getPilotTestData();
when(personClient.storeV1(any(UUID.class), any(Person.class))).thenReturn(Single.just(HttpResponse.created(person.getId())));
//This is where I am making the call
final Single<HttpResponse<UUID>> responseSingle = pilotTestClient.createPilotV1(UUID.randomUUID(), "web", "12345", pilot);
final HttpResponse<UUID> response = responseSingle.blockingGet();
assertAll(() -> {
assertNotNull(response);
assertEquals(response.getBody().get(), person.getId());
});
verify(personClient).storeV1(any(UUID.class), any(Person.class));
}
}