RestTemplate reads String data with "\" content (Even with StringHttpMessageConverter) - spring

I am working with Spring Framework 4.3.6.RELEASE
For testing purposes through RestTemplate how a consumer, if the HTTP DELETE method fails because the entity does not exist, I need return an error message in raw format or String. It because 'normally' DELETE does not require mandatorily neither Accept nor Content-Type. I don't want complicate the user setting the Accept either for XML or JSON
My #Test method fails with the following error
java.lang.AssertionError:
Expected: is "The entity does not exist"
but: was "\"The entity does not exist\""
Observe the \" part
In the server side, I have declared:
#Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
StringHttpMessageConverter converter = new StringHttpMessageConverter();
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Arrays.asList(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8)));
return converter;
}
And is registered:
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(httpMessageConverterConfig.mappingJackson2HttpMessageConverter());
converters.add(httpMessageConverterConfig.mappingJackson2XmlHttpMessageConverter());
converters.add(httpMessageConverterConfig.stringHttpMessageConverter());
}
With a #ControllerAdvice, working together with a method annotated with #ExceptionHandler, a ResponseEntity<Object> object is created with the following approach in some place to establish the Content-Type
if(httpServletRequest.getMethod().equals(HttpMethod.DELETE.toString())){
headers.setContentType(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8));
}
When I print the error message in the server side it prints without \" part
The RestTemplate for testing purposes is created with:
RestTemplate restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
restTemplate.setMessageConverters(customConverters);
restTemplate.setErrorHandler(restTemplateResponseErrorHandler);
Therefore why the \" part arrives to RestTemplate?
How it can be fix it.? What missing configuration is need it?
Alpha
Test Class declaration
#Transactional
#WebAppConfiguration
#RunWith(Parameterized.class)
#ActiveProfiles(resolver=TestActiveProfilesResolver.class)
#ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class})
#TestExecutionListeners(listeners={LoggingTestExecutionListener.class}, mergeMode=MergeMode.MERGE_WITH_DEFAULTS)
public class PersonaDeleteOneRestControllerClientBadRequestTest {
Some variables in that class:
#Autowired
private WebApplicationContext webApplicationContext;
#Autowired
private RestTemplateClientSupport restTemplateClientSupport;
private MockMvc mockMvc;
private RestTemplate restTemplate;
private static RestTemplateResponseErrorHandler restTemplateResponseErrorHandler;
Some creations:
#BeforeClass
public static void setUp_(){
restTemplateResponseErrorHandler = new RestTemplateResponseErrorHandler();
}
#Before
public void setUp(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
restTemplate = restTemplateClientSupport.deleteOneBadRequest(mockMvc);
restTemplate.setErrorHandler(restTemplateResponseErrorHandler);
}
The RestTemplate part is:
#Component
public class RestTemplateClientSupport {
private static final Logger logger = LoggerFactory.getLogger(RestTemplateClientSupport.class.getSimpleName());
private final List<HttpMessageConverter<?>> customConverters;
#Autowired
public RestTemplateClientSupport(List<HttpMessageConverter<?>> customConverters){
this.customConverters = customConverters;
}
private RestTemplate createRestTemplateWithListHttpMessageConverter(MockMvc mockMvc){
RestTemplate restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
restTemplate.getMessageConverters().removeIf(converter -> {
if(converter.getClass().getSimpleName().equals(MappingJackson2XmlHttpMessageConverter.class.getSimpleName())){
logger.info("Removing: {}" , converter.getClass().getSimpleName());
return true;
}
else{
return false;
}
});
restTemplate.getMessageConverters().removeIf(converter -> {
if(converter.getClass().getSimpleName().equals(MappingJackson2HttpMessageConverter.class.getSimpleName())){
logger.info("Removing: {}" , converter.getClass().getSimpleName());
return true;
}
else{
return false;
}
});
restTemplate.getMessageConverters().removeIf(converter -> {
if(converter.getClass().getSimpleName().equals(StringHttpMessageConverter.class.getSimpleName())){
logger.info("Removing: {}" , converter.getClass().getSimpleName());
return true;
}
else{
return false;
}
});
restTemplate.getMessageConverters().addAll(customConverters);
return restTemplate;
Note: I am using restTemplate.getMessageConverters().removeIf(Predicate<>.. because I need remove these 3 default converters and add my own one created.

Related

How to mock methods which has parameters with #Value annotated class/member variables?

I have 3 classes involved in my problem: MyService, MyServiceTest, MyConfig and a yaml file application.yml. MyRequest and MyResponse are my custom classes which denotes the incoming request to service class from controller and outgoing response from service class to controller respectively.
I am getting below error:
SAAJ007: Bad Endpoint type
application.yml
backside:
url: https://mybackend.com/my/operation
ssl:
store: D:\\mydata\\myconfiguration\\mykey.jks
token: password
MyService.java
#Service
public class MyService{
#Value("${backside.url}")
private String backsideUrl;
private MyFormatter formatter; // has methods which formats request and response
private SOAPConnectionFactory soapFactory;
#Autowired
public MyService(MyFormatter formatter, SOAPConnectionFactory soapFactory){
this.formatter = formatter;
this.soapConnectionFactory = soapConnectionFactory;
}
public MyResponse createSomething(MyRequest request){
MyResponse response;
try{
SOAPConnection connection = soapConnectionFactory.createConnection();
SOAPMessage soapResponse = connection.call(formatter.formatRequest(request), backsideUrl); // formatRequest(MyRequest request) returns object of SOAPMessage
response = formatter.formatResponse(soapResponse); // formatResponse(SOAPMessage soapMsg) returns object of MyResponse
connection.close();
}
catch(SOAPException ex){
logger.error("SOAP Connection Error:{}", ex.toString());
throw MyCustomException("MyErrorMsg", HttpStatus.INTERNAL_SERVER_ERROR);
}
catch(Exception ex){
logger.error("Unknown Error:{}", ex.toString());
throw MyCustomException("UnknownErrorMsg", HttpStatus.INTERNAL_SERVER_ERROR);
}
return response;
}
}
MyConfiguration.java
#Configuration
public class MyConfiguration{
#Value("${ssl.store}")
private String keyPath;
#Value("${ssl.token}")
private String keyPassword;
#Bean
public SOAPConnectionFactory soapConnectionFactory() throws SOAPException{
return SOAPConnectionFactory.newInstance();
}
#Bean
public void addSSLCertificate() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithException, UnrecoverableKeyException, KeyManagementException{
// lines of code which uses #Value annotated keyPath and keyPassword fields
}
}
MyServiceTest.java
#ExtendWith(SpringExtension.class)
public class MyServiceTest{
#Mock
private SOAPConnectionFactory soapConnectionFactory;
#Mock
private SOAPConnection soapConnection;
#Mock
private MyFormatter formatter;
#InjectMocks
private MyService service;
private MyRequest request;
private MyResponse expectedResponse;
private SOAPMessage soapRequest;
private SOAPMessage soapResponse;
#BeforeEach
void initializeObjects() throws SOAPException{
// Lines of code creating an object of MyRequest and setting its field values : request
// Lines of code creating an object of MyResponse and setting its field values : expectedResponse
// Lines of code creating an object of SOAPMessage: soapRequest
// Lines of code creating an object of SOAPMessage: soapResponse
}
#Test
public void myTestCase(){
Mockito.when(soapConnectionFactory.createConnection()).thenReturn(soapConnection);
Mockito.when(formatter.formatRequest(any(MyRequest.class))).thenReturn(soapRequest);
Mockito.when(soapConnection.call(any(SOAPMessage.class), any(String.class))).thenReturn(soapResponse);
Mockito.when(formatter.formatResponse(any(SOAPMessage.class))).thenReturn(expectedResponse);
MyResponse actualResponse = service.createSomething(request);
Assertions.assertEquals(expectedResponse.toString(), actualResponse.toString()); // overridden toString() method exists in the definition of MyResponse class
}
}
Since the value of String object i.e. backsideUrl is not mocked and hence it will not get assigned, then at the time of connection.call(formatter.formatRequest(request), backsideUrl) method execution, the value of backsideUrl will be null. So the mock is not going to working properly because we are using any(String.class).
Instead of this:
Mockito.when(soapConnection.call(any(SOAPMessage.class), any(String.class))).thenReturn(soapResponse);
Use this:
Mockito.when(soapConnection.call(any(SOAPMessage.class), any())).thenReturn(soapResponse);

#Mock RestTemplate not returning expected value

I am creating unit test for my service class:
#Service
#Slf4j
public class SsaOpeningService {
#Autowired
private MockDataInitializer mockDataInitializer;
#Qualifier("restHttpsTemplateClient")
#Autowired
private RestTemplate restTemplate;
#Value("${acc-opening-casa.open-ssa}")
private String openSSAaccountUrl;
public CompletableFuture<AccountData> openSsa(
ApplicationDto items,
HttpHeaders headers,
BigInteger cifNo) {
log.info("Initializing headers");
HeaderRequest headerRequest = new HeaderRequest();
HttpHeaders header = headerRequest.initHeader(headers);
CurrentAcctReqBody request = CurrentAcctReqBody.builder()
.cifNo(cifNo)
.currencyType(SGD_CURRENCY)
.acName1(items.getApplicationData().getPersonalDetail().getName())
.productType(SSA_PRODUCT_CODE)
.noOfAccountHolders(BigInteger.ONE)
.accountType(ACC_TYPE)
.transactionRefNo(mockDataInitializer.randomIntInString(9))
.build();
log.info("Setting up entity for calling SSA opening.....");
HttpEntity<CurrentAcctReqBody> entity = new HttpEntity<>(request, header);
CurrentAcctResBody result = null;
try {
result = restTemplate
.postForObject(openSSAaccountUrl, entity, CurrentAcctResBody.class);
} catch (Exception e) {
log.info(e.getMessage());
}
System.out.println(14527);
System.out.println(result);
if(result !=null && result.getError()==null) {
AccountData accountData = AccountData.builder().build();
BeanUtils.copyProperties(result.getRbkAccountDetail(), accountData);
return CompletableFuture.completedFuture(accountData);
}
return null;
}
}
My test class:
#SpringBootTest
#RunWith(SpringRunner.class)
class SsaOpeningServiceTest {
#InjectMocks private SsaOpeningService ssaOpeningService;
#Autowired private MockDataInitializer dataInitializer;
#Mock private MockDataInitializer mockDataInitializer;
private static HttpHeaders headers = new HttpHeaders();
private static HeaderRequest ekycHeaderRequest = new HeaderRequest();
#BeforeAll
public static void init() {
headers = ekycHeaderRequest.initHeader();
}
#Qualifier("restHttpsTemplateClient")
#Mock private RestTemplate restTemplate;
#Test
void createSsa() throws IOException {
CurrentAcctResBody result = JSONUtils
.getObjectFromJson(DCResourceLoader.getResourceAsString("casa/ssa-res.json"), CurrentAcctResBody.class);
ApplicationDto items = ApplicationDto.builder().build();
Application application = dataInitializer.initialize();
BeanUtils.copyProperties(application, items);
when(restTemplate.postForObject(
any(String.class),
eq(HttpEntity.class),
eq(CurrentAcctResBody.class)))
.thenReturn(result);
System.out.println(1452);
System.out.println(result);
when(mockDataInitializer.randomIntInString(any(Integer.class)))
.thenReturn(dataInitializer.randomIntInString(9));
assertThat(ssaOpeningService.openSsa(items, headers, any(BigInteger.class))).isNull();
}
}
I have mocked my RestTemplate to return me the result I want. Problem is, it is not giving me the expected result. I have printed the result in both test class and service class. But in the service class it is always giving me null. I tried to give the most generic parameter when mocking but still doesnt work. The rest is working fine when running unit test EXCEPT for this part. Need assist on this. Thanks all!
#RunWith(MockitoJUnitRunner.class)
class SsaOpeningServiceTest {
#InjectMocks
private SsaOpeningService ssaOpeningService;
#Mock
private MockDataInitializer mockDataInitializer;
#Mock
private RestTemplate restTemplate;
#Test
void createSsa() throws IOException {
// some code
Mockito.when(restTemplate.postForObject(
nullable(String.class),
any(HttpEntity.class),
eq(CurrentAcctResBody.class)))
.thenReturn(result);
// some code
}
}

How to mock RestTemplate in Springboot app

This question has already been asked. The accepted answer doesn't work for me. Here is my code:-
My service is here:
#Service
public class PlantService {
#Autowired
RestTemplate restTemplate;
static String url = "http://some_url_?Combined_Name=Oak";
#Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
public String getJson() {
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return response.getBody();
}
}
My unit test
#RunWith(SpringRunner.class)
class PlantServiceTest {
private PlantService plantService;
#Mock
#Autowired
private RestTemplate restTemplate;
#Before
void setUp() {
MockitoAnnotations.initMocks(this);
plantService = new PlantService();
}
#Test
void testGetJsonString() {
// arrange
String expectedJson = "Some json string";
ResponseEntity mocResponse = mock(ResponseEntity.class);
// act
when(restTemplate.getForEntity("url", String.class)).thenReturn(mocResponse);
String actualJson = plantService.getJson();
// assert
assertSame(expectedJson, actualJson);
}
}
When I debug and step into the actual code. I can see restTemplate is null and throws java.lang.NullPointerException. So how do I unit test this code?
I have tried your code running on my machine.
Please find the test running test class
#RunWith(SpringRunner.class)
class PlantServiceTest {
#InjectMocks
private PlantService plantService;
#Mock
private RestTemplate restTemplate;
String url = "http://some_url_?Combined_Name=Oak";
#BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
}
#Test
void testGetJsonString() {
// arrange
String expectedJson = "Some json string";
ResponseEntity mocResponse = new ResponseEntity("Some json string", HttpStatus.OK);
// act
when(restTemplate.getForEntity(url, String.class)).thenReturn(mocResponse);
String actualJson = plantService.getJson();
// assert
assertSame(expectedJson, actualJson);
}
}
You can do the following:
Remove #RunWith annotation.
Annontate your test class with #RestClientTest from org.springframework.boot.test.autoconfigure.web.client.RestClientTest.
Use MockRestServiceServer from org.springframework.test.web.client.MockRestServiceServer.
Mock the response of the server when being called in the test method, example:
#RestClientTest
public class MyTest {
#Autowired
private MockRestServiceServer server;
public void test() {
// setup
String expected = "test_value";
server.expect(requestToUriTemplate("/myendpoint"))
.andRespond(withSuccess(myJsonResponse, MediaType.APPLICATION_JSON));
// act
String actual = myClient.fetch(myRequestDto);
// assert
assertThat(actual, equalTo(expected));
server.verify();
}
}
I'm using assertThat from hamcrest, you can use whatever you want to assert the correctness of the result.
You should use constructor injection in your PlantService. For instance:
public class PlantService {
RestTemplate restTemplate;
#Autowired
public PlantService(RestTemplate restTemplate){
this.restTemplate = restTemplate;
}
}
And on your test you can just do:
plantService = new PlantService(restTemplate);
^^
yourMock
Your problem is the plantService = new PlantService(); You never inject into this selft created instance.
Solution 1
I usually do it like this:
#InjectMocks
private PlantService plantService = new PlantService();
#Mock
private RestTemplate restTemplate;
Remove the setup method and run with Mockito instead of the SpringRunner.
Solution 2
If you need the SpringRunner you can do the following:
#BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
plantService = new PlantService();
ReflectionTestUtils.setField(plantService, "restTemplate", restTemplate);
}
As I've worked with JUnit 5 in the last years, I'm not sure about the SpringRunner. In JUnit 5 I can use both extensions (Spring and Mockito at the same time). Maybe this also worked in JUnit 4.

Using #RestClientTest in spring boot test

I want to write a simple test using #RestClientTest for the component below (NOTE: I can do it without using #RestClientTest and mocking dependent beans which works fine.).
#Slf4j
#Component
#RequiredArgsConstructor
public class NotificationSender {
private final ApplicationSettings settings;
private final RestTemplate restTemplate;
public ResponseEntity<String> sendNotification(UserNotification userNotification)
throws URISyntaxException {
// Some modifications to request message as required
return restTemplate.exchange(new RequestEntity<>(userNotification, HttpMethod.POST, new URI(settings.getNotificationUrl())), String.class);
}
}
And the test;
#RunWith(SpringRunner.class)
#RestClientTest(NotificationSender.class)
#ActiveProfiles("local-test")
public class NotificationSenderTest {
#MockBean
private ApplicationSettings settings;
#Autowired
private MockRestServiceServer server;
#Autowired
private NotificationSender messageSender;
#Test
public void testSendNotification() throws Exception {
String url = "/test/notification";
UserNotification userNotification = buildDummyUserNotification();
when(settings.getNotificationUrl()).thenReturn(url);
this.server.expect(requestTo(url)).andRespond(withSuccess());
ResponseEntity<String> response = messageSender.sendNotification(userNotification );
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
private UserNotification buildDummyUserNotification() {
// Build and return a sample message
}
}
But i get error that No qualifying bean of type 'org.springframework.web.client.RestTemplate' available. Which is right of course as i havn't mocked it or used #ContextConfiguration to load it.
Isn't #RestClientTest configures a RestTemplate? or i have understood it wrong?
Found it! Since i was using a bean that has a RestTemplate injected directly, we have to add #AutoConfigureWebClient(registerRestTemplate = true) to the test which solves this.
This was in the javadoc of #RestClientTest which i seem to have ignored previously.
Test which succeeds;
#RunWith(SpringRunner.class)
#RestClientTest(NotificationSender.class)
#ActiveProfiles("local-test")
#AutoConfigureWebClient(registerRestTemplate = true)
public class NotificationSenderTest {
#MockBean
private ApplicationSettings settings;
#Autowired
private MockRestServiceServer server;
#Autowired
private NotificationSender messageSender;
#Test
public void testSendNotification() throws Exception {
String url = "/test/notification";
UserNotification userNotification = buildDummyUserNotification();
when(settings.getNotificationUrl()).thenReturn(url);
this.server.expect(requestTo(url)).andRespond(withSuccess());
ResponseEntity<String> response = messageSender.sendNotification(userNotification );
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
private UserNotification buildDummyUserNotification() {
// Build and return a sample message
}
}

SpringBoot Junit testing for filters in Zuul

I'm new to Zuul J-unit testing. I have a couple of filters which is ChangeRequestEntityFilter and SessionFilter, Where I pasted my filtercode below. Can someone tell me how to write a Junit for the filter. I've searched and trying to use MockWire for the unit testing(Also I pasted my empty methods with basic annotations and WireMock port). I need at-least one proper example how this J-unit for Zuul works. I've referred the http://wiremock.org/docs/getting-started/ doc. Where I got what to do, but not how to do.
public class ChangeRequestEntityFilter extends ZuulFilter {
#Autowired
private UtilityHelperBean utilityHelperBean;
#Override
public boolean shouldFilter() {
// //avoid http GET request since it does'nt have any request body
return utilityHelperBean.isValidContentBody();
}
#Override
public int filterOrder() {
//given priority
}
#Override
public String filterType() {
// Pre
}
#Override
public Object run() {
RequestContext context = getCurrentContext();
try {
/** get values profile details from session */
Map<String, Object> profileMap = utilityHelperBean.getValuesFromSession(context,
CommonConstant.PROFILE.value());
if (profileMap != null) {
/** get new attributes need to add to the actual origin microservice request payload */
Map<String, Object> profileAttributeMap = utilityHelperBean.getProfileForRequest(context, profileMap);
/** add the new attributes in to the current request payload */
context.setRequest(new CustomHttpServletRequestWrapper(context.getRequest(), profileAttributeMap));
}
} catch (Exception ex) {
ReflectionUtils.rethrowRuntimeException(new IllegalStateException("ChangeRequestEntityFilter : ", ex));
}
return null;
}
}
I know ,I'm asking more. But give me any simple working complete example, I'm fine with it.
My current code with basic annotations and WireMock port.
#RunWith(SpringRunner.class)
#SpringBootTest
#DirtiesContext
#EnableZuulProxy
public class ChangeRequestEntityFilterTest {
#Rule
public WireMockRule wireMockRule = new WireMockRule(8080);
#Mock
ChangeRequestEntityFilter requestEntityFilter;
int port = wireMockRule.port();
#Test
public void changeRequestTest() {
}
}
Have you tried #MockBean?
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html
"When #MockBean is used on a field, as well as being registered in the application context, the mock will also be injected into the field. Typical usage might be:"
#RunWith(SpringRunner.class)
public class ExampleTests {
#MockBean
private ExampleService service;
#Autowired
private UserOfService userOfService;
#Test
public void testUserOfService() {
given(this.service.greet()).willReturn("Hello");
String actual = this.userOfService.makeUse();
assertEquals("Was: Hello", actual);
}
#Configuration
#Import(UserOfService.class) // A #Component injected with ExampleService
static class Config {
}
}
Here there is another approach:
private ZuulPostFilter zuulPostFilter;
#Mock
private anotherService anotherService;
#Mock
private HttpServletRequest request;
#Before
public void before() {
MockitoAnnotations.initMocks(this);
MonitoringHelper.initMocks();
zuulPostFilter = new ZuulPostFilter(anotherService);
doNothing().when(anotherService).saveInformation(null, false);
}
#Test
public void postFilterTest() {
log.info("postFilterTest");
RequestContext context = new RequestContext();
context.setResponseDataStream(new ByteArrayInputStream("Test Stream".getBytes()));
context.setResponseGZipped(false);
RequestContext.testSetCurrentContext(context);
when(request.getScheme()).thenReturn("HTTP");
RequestContext.getCurrentContext().setRequest(request);
ZuulFilterResult result = zuulPostFilter.runFilter();
assertEquals(ExecutionStatus.SUCCESS, result.getStatus());
assertEquals("post", zuulPostFilter.filterType());
assertEquals(10, zuulPostFilter.filterOrder());
}
In this case you can test the filter and mock the services inside it without having to autowire it, the problem with the #autowired is that if you have services inside the filter, then it is going to be an integration test that is going to be more difficult to implement.

Resources