I am using Spring Boot 2.1.2 and junit-jupiter-api-5.3.2 and I am doing integration testing.
My test case is this:
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.ril.vms.hawkeye.otp.dto.SendOTPRequestDTO;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(classes = MyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("sit")
public class OtpControllerIntegrationTest
{
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void sendOtpTest() throws Exception {
SendOTPRequest otpRequest = new SendOTPRequestDTO();
otpRequest.setClientId("default2");
otpRequest.setTag("tag");
otpRequest.setMobileNumber("9999999999");
ResponseEntity<String> responseEntity = this.restTemplate
.postForEntity("http://localhost:" + port + "/sendOtp", otpRequest, String.class);
assertEquals(201, responseEntity.getStatusCodeValue());
}
}
But I am getting java.lang.NullPointerException
at below line.
ResponseEntity<String> responseEntity = this.restTemplate ...