How to init an object in a JUnit test? - java

I am trying to do a JUnit test to test a Controller.
My Controller consists of:
#Autowired
private OrderManager orderManager;
#RequestMapping(value = "/getOrderList", method = RequestMethod.POST)
#ResponseBody
public Map<String, Object> getOrderTables(OrderSearchDto orderSearchDto) {
...
orderHdrTables = orderManager.findAllOrders(orderSearchDto);
...
}
and my test is:
#RunWith(MockitoJUnitRunner.class)
public class FilterActivityTest {
#InjectMocks
private OrderHdrController orderHdrController;
private MockMvc mockMvc;
#Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(orderHdrController).build();
}
#Test
public void testActivityFilter() throws Exception {
OrderSearchDto orderSearchDto = new OrderSearchDto();
OrderSearchPanelDto orderSearchPanelDto = new OrderSearchPanelDto();
List <String> activityTypes = Arrays.asList("DELIVERY","START_UPLOAD");
orderSearchPanelDto.setActivityTypes(activityTypes);
orderSearchDto.setOrderSearchPanelDto(orderSearchPanelDto);
mockMvc.perform(MockMvcRequestBuilders.post("/orders/getOrderList")
.content(asJsonString(orderSearchDto))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)).andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.data[0].tripNo", is("SG1606LLR08469")))
.andExpect(jsonPath("$.data.data[1].tripNo", is("SG1606LLR08470")));;
}
private static String asJsonString(final Object obj) {
try {
final ObjectMapper mapper = new ObjectMapper();
final String jsonContent = mapper.writeValueAsString(obj);
return jsonContent;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
But during debug of the test, I am getting orderManager is null in my Controller. What can I do to initialise it?

I have modified your test a bit. Please try this.
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = {
FilterActivityTest.Config.class,
OrderHdrController.class,
})
public class FilterActivityTest {
#Configuration
static class Config {
#Bean
OrderManager orderManager() {
return mock(OrderManager.class);
}
}
#Autowired
private OrderHdrController orderHdrController;
#Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(orderHdrController).build();
//remember to mock orderManager.findAllOrders here
}
}

You need to initialize the mocks
Update your #BeforeEach method like this:
#Mock
private OrderManager orderManager;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(orderHdrController).build();
}
Also, you may not need #RunWith(MockitoJUnitRunner.class)

Related

Junit5 test case for controller layer

#GetMapping(value = "/abc")
public ResponseDto<Map<String, FResponseDto>, Void> getFlightDetails(#RequestParam String fIds) {
Map<String, FResponseDto> response = fService
.getDetails(fIds);
if (Objects.isNull(response)) {
return ResponseDto.failure(FConstants.FLIGHT_NOT_FOUND);
}
return ResponseDto.success(FConstants.SUCCESS,response);
}
how to test this other then status i want to test the if and other return
This code may help you.
#WebMvcTest(controllers = YourController.class)
class YourControllerTest {
private MockMvc mockMvc;
#Autowired
private YourController controller;
#MockBean
private FService fService;
#BeforeEach
public void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(controller)
.build();
}
#Test
public void test_getFlightDetails() throws Exception {
Map<String, FResponseDto> data = ......;
Mockito.when(fService.getDetails(anyString())).thenReturn(data);
this.mockMvc.perform(get("/abc"))
.andExpect(status().isOk());
Mockito.when(fService.getDetails(anyString())).thenReturn(null);
this.mockMvc.perform(get("/abc"))
.andExpect(status().is5xxServerError());
}
}

No qualifying bean of type 'org.springframework.boot.actuate.health.HealthEndpoint' in controller test

I wrote a controller which combines actuator info.
#RestController
#Slf4j
public class AppStatusRestController {
private final HealthEndpoint healthEndpoint;
private final InfoEndpoint infoEndpoint;
public AppStatusRestController(HealthEndpoint healthEndpoint, InfoEndpoint infoEndpoint) {
this.healthEndpoint = healthEndpoint;
this.infoEndpoint = infoEndpoint;
}
#GetMapping("/status")
public Status status() {
Map<String, Object> info = infoEndpoint.info();
return Status.builder()
.status(healthEndpoint.health().getStatus().getCode())
.appName("My application")
.version(((Map<String, Object>) info.get("build")).get("version").toString())
.buildDateTime(((Map<String, Object>) info.get("build")).get("timestamp").toString())
.build();
}
}
In my test I get an error No qualifying bean of type 'org.springframework.boot.actuate.health.HealthEndpoint'.
#SpringBootTest(classes = AppStatusRestController.class)
#TestPropertySource(properties = "management.endpoints.web.exposure.include=*")
class AppStatusRestControllerTest {
#Test
void status() {
}
}
How can I actiavate default spring actuator beans in controller test(#SpringBootTest/#WebMvcTest)?
I guess I'm narrowing down the context is the answer to your question: Spring includes only the controller into its context skipping everything else. Try to include HealthEndpointAutoConfiguration too.
You can use #SpringBootTest() with no classes and it will work too. In this case, Spring will load the full context for executing the tests.
#SpringBootTest()
#AutoConfigureMockMvc
#TestPropertySource(properties = { "management.endpoints.web.exposure.include=health,info" })
class AppStatusRestControllerTest {
#Autowired
private MockMvc mockMvc;
#Test
void version() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/status")).andReturn();
String contentAsString = mvcResult.getResponse().getContentAsString();
ObjectMapper objectMapper = new ObjectMapper();
Status result = objectMapper.readValue(contentAsString, Status.class);
assertEquals(result.getStatus(), "UP");
}
}
I fixed the problem by creating TestApplication.java which includes only one controller of my application. Then I'm testing by loading test app into context #SpringBootTest(classes = TestApplication.class).
#SpringBootApplication
#Import(AppStatusRestController.class)
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
#SpringBootTest(classes = TestApplication.class)
#AutoConfigureMockMvc
#TestPropertySource(properties = { "management.endpoints.web.exposure.include=health,info" })
class AppStatusRestControllerTest {
#Autowired
private MockMvc mockMvc;
#Test
void version() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/status")).andReturn();
String contentAsString = mvcResult.getResponse().getContentAsString();
ObjectMapper objectMapper = new ObjectMapper();
Status result = objectMapper.readValue(contentAsString, Status.class);
assertEquals(result.getStatus(), "UP");
}
}

No mapping for request with mockmvc

Currently struggling with problem when I get 'mapping error for request' with following controller/test configuration.
Controller:
#Slf4j
#Validated
#RestController
#RequiredArgsConstructor
public class AdtechController {
private final AdtechService adtechService;
#PostMapping(value = "/subscriber/session")
public ResponseEntity<ResponseDto> submitSession(#RequestBody RequestDto requestDto) {
log.trace("execute submitSession with {}", requestDto);
ResponseDtoresponse = adtechService.submitSession(requestDto);
return new ResponseEntity<>(response, HttpStatus.OK);
}
#ExceptionHandler(AdtechServiceException.class)
public ResponseEntity<AdtechErrorResponse> handleAdtechServiceException(AdtechServiceException e) {
return new ResponseEntity<>(new AdtechErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Test:
#RunWith(SpringRunner.class)
#SpringBootTest
#AutoConfigureMockMvc
#SpringJUnitConfig({AdtechTestConfig.class})
public class AdtechControllerTest {
private static final ObjectMapper OBJECT_MAPPER = JsonUtil.getJackson();
#Autowired
private MockMvc mockMvc;
#Test
public void testSubmitSession() throws Exception {
RequestDto requestDto = new RequestDto ();
requestDto.setKyivstarId("1123134");
requestDto.setMsisdn("123476345242");
requestDto.setPartnerId("112432523");
requestDto.setPartnerName("125798756");
String request = OBJECT_MAPPER.writeValueAsString(requestDto);
System.out.println("REQUEST: " + request);
String response = OBJECT_MAPPER.writeValueAsString(new ResponseDto("123"));
System.out.println("RESPONSE: " + response);
mockMvc.perform(post("/subscriber/session")
.content(MediaType.APPLICATION_JSON_VALUE)
.content(request))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString(response)));
}
}
Configuration:
#Configuration
public class AdtechTestConfig {
#Bean
public AdtechService adtechTestService() {
return requestDto -> new AdtechResponseDto("123");
}
}
After test execution I get No mapping for POST /subscriber/session
The reason for the struggle is that my code from other modules with the same configuration works fine. Can somebody point out what am I missing ? Thanks in advance!
Apparently you are loading a configuration class to mock beans, this interferes with the other parts of Spring Boot and probably leads to partially loading your application. I suspect only the mocked service is available.
Instead of the test configuration use #MockBean to create a mock for the service and register behaviour on it.
#SpringBootTest
#AutoConfigureMockMvc
public class AdtechControllerTest {
private static final ObjectMapper OBJECT_MAPPER = JsonUtil.getJackson();
#Autowired
private MockMvc mockMvc;
#MockBean
private AdtechService mockService;
#BeforeEach
public void setUp() {
when(mockService.yourMethod(any()).thenReturn(new AdtechResponseDto("123"));
}
#Test
public void testSubmitSession() throws Exception {
// Your original test method
}
}
If the only thing you want to test is your controller you might also want to consider using #WebMvcTest instead of #SpringBootTest.
#WebMvcTest(AdTechController.class)
public class AdtechControllerTest {
private static final ObjectMapper OBJECT_MAPPER = JsonUtil.getJackson();
#Autowired
private MockMvc mockMvc;
#MockBean
private AdtechService mockService;
#BeforeEach
public void setUp() {
when(mockService.yourMethod(any()).thenReturn(new AdtechResponseDto("123"));
}
#Test
public void testSubmitSession() throws Exception {
// Your original test method
}
}
This will load a scaled-down version of the context (only the web parts) and will be quicker to run.
try this:
#Slf4j
#Validated
#RestController
#RequiredArgsConstructor
public class AdtechController {
private AdtechService adtechService;
public AdtechController (AdtechService adtechService) {
this.adtechService= adtechService;
}
#PostMapping(value = "/subscriber/session")
public ResponseEntity<ResponseDto> submitSession(#RequestBody RequestDto requestDto) {
log.trace("execute submitSession with {}", requestDto);
ResponseDtoresponse = adtechService.submitSession(requestDto);
return new ResponseEntity<>(response, HttpStatus.OK);
}
#ExceptionHandler(AdtechServiceException.class)
public ResponseEntity<AdtechErrorResponse> handleAdtechServiceException(AdtechServiceException e) {
return new ResponseEntity<>(new AdtechErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Test:
#RunWith(SpringRunner.class)
#SpringBootTest
#AutoConfigureMockMvc
#SpringJUnitConfig({AdtechTestConfig.class})
public class AdtechControllerTest {
private static final ObjectMapper OBJECT_MAPPER = JsonUtil.getJackson();
#Autowired
private MockMvc mockMvc;
#Autowired
private AdtechService adtechService;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mvc = MockMvcBuilders.standaloneSetup(new AdtechController(adtechService)).build();
}
#Test
public void testSubmitSession() throws Exception {
RequestDto requestDto = new RequestDto ();
requestDto.setKyivstarId("1123134");
requestDto.setMsisdn("123476345242");
requestDto.setPartnerId("112432523");
requestDto.setPartnerName("125798756");
String request = OBJECT_MAPPER.writeValueAsString(requestDto);
System.out.println("REQUEST: " + request);
String response = OBJECT_MAPPER.writeValueAsString(new ResponseDto("123"));
System.out.println("RESPONSE: " + response);
mockMvc.perform(post("/subscriber/session")
.content(MediaType.APPLICATION_JSON_VALUE)
.content(request))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString(response)));
}
}
Is the AdtechTestConfig.class introducing the /ad-tech path segment in to your test request? If so, this is why your test is trying the path /ad-tech/subscriber/session instead of /subscriber/session.
If this is actually the correct uri, then you may add #RequestMapping to the controller like below or just to the post method itself
#Slf4j
#Validated
#RestController
#RequestMapping("/ad-tech")
#RequiredArgsConstructor
public class AdtechController {
private final AdtechService adtechService;
#PostMapping(value = "/subscriber/session")
public ResponseEntity<ResponseDto> submitSession(#RequestBody RequestDto requestDto) {
...

java.lang.AssertionError: Status expected:<200> but was:<404> in Junit test

I want to create JUnit test for Rest api and generate api doc. I want to test this code:
Rest controller
#RestController
#RequestMapping("/transactions")
public class PaymentTransactionsController {
#Autowired
private PaymentTransactionRepository transactionRepository;
#GetMapping("{id}")
public ResponseEntity<?> get(#PathVariable String id) {
return transactionRepository
.findById(Integer.parseInt(id))
.map(mapper::toDTO)
.map(ResponseEntity::ok)
.orElseGet(() -> notFound().build());
}
}
Repository interface
public interface PaymentTransactionRepository extends CrudRepository<PaymentTransactions, Integer>, JpaSpecificationExecutor<PaymentTransactions> {
Optional<PaymentTransactions> findById(Integer id);
}
I tried to implement this JUnit5 test with mockito:
#ExtendWith({ RestDocumentationExtension.class, SpringExtension.class })
#SpringBootTest(classes = PaymentTransactionsController.class)
#WebAppConfiguration
public class PaymentTransactionRepositoryIntegrationTest {
.....
private MockMvc mockMvc;
#MockBean
private PaymentTransactionRepository transactionRepository;
#BeforeEach
void setUp(WebApplicationContext webApplicationContext,
RestDocumentationContextProvider restDocumentation) {
PaymentTransactions obj = new PaymentTransactions(1);
Optional<PaymentTransactions> optional = Optional.of(obj);
PaymentTransactionRepository processor = Mockito.mock(PaymentTransactionRepository.class);
Mockito.when(processor.findById(Integer.parseInt("1"))).thenReturn(optional);
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply(documentationConfiguration(restDocumentation))
.alwaysDo(document("{method-name}", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint())))
.build();
}
#Test
public void testNotNull() {
assertNotNull(target);
}
#Test
public void testFindByIdFound() {
Optional<PaymentTransactions> res = target.findById(Integer.parseInt("1"));
// assertTrue(res.isPresent());
}
#Test
public void indexExample() throws Exception {
this.mockMvc.perform(get("/transactions").param("id", "1"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/xml;charset=UTF-8"))
.andDo(document("index-example", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), links(linkWithRel("crud").description("The CRUD resource")), responseFields(subsectionWithPath("_links").description("Links to other resources")),
responseHeaders(headerWithName("Content-Type").description("The Content-Type of the payload, e.g. `application/hal+json`"))));
}
}
I get error:
java.lang.AssertionError: Status expected:<200> but was:<404>
What his the proper way to to make GET request to the above code?
Probably I need to add response OK when message is send back?
hi in my case i needed #MockBean of controller and all services that is was autowireing ;)
Instead of #PostMapping and #GetMapping which caused same problem while #RequestMapping in controller helped
It's a path variable, so instead of using param value, please use path variable.
For MvcResult import, you can import org.springframework.test.web.servlet
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
...
given(target.findById(anyInt())).willReturn(Optional.of(new PaymentTransactions(1))).andReturn();
MvcResult result = this.mockMvc.perform(get("/transactions/1")
.accept("application/xml;charset=UTF-8")).andReturn();
String content = result.getResponse().getContentAsString();
this.mockMvc.perform(get("/transactions/1")
.accept("application/xml;charset=UTF-8"))
.andExpect(status().isOk())
.andDo(document("index-example", preprocessRequest(prettyPrint()), preprocessResponse(prettyPrint()), links(linkWithRel("crud").description("The CRUD resource")), responseFields(subsectionWithPath("_links").description("Links to other resources")),
responseHeaders(headerWithName("Content-Type").description("The Content-Type of the payload, e.g. `application/hal+json`"))));
Can you try this..
public class PaymentTransactionsControllerTest {
private MockMvc mvc;
#InjectMocks
PaymentTransactionsController paymentTransactionsController;
#MockBean
private PaymentTransactionRepository processor;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mvc = MockMvcBuilders.standaloneSetup(paymentTransactionsController).build();
}
#Test
public void indexExample() throws Exception {
PaymentTransactions obj = new PaymentTransactions(1);
Optional<PaymentTransactions> optional = Optional.of(obj);
Mockito.when(processor.findById(Integer.parseInt("1"))).thenReturn(optional);
MvcResult result = mvc.perform(MockMvcRequestBuilders.get("/transactions/{id}", 1))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
Assert.assertNotNull(result.getResponse().getContentAsString());
}
}

Junit Test cases for Spring MVC 4 controller to check #PathVariable parameters in #RequestMapping

I have following controller code for which I have to write JUnit test case.
public class EquipmentController {
private Map<String, Equipment> equiList = new HashMap <String,Equipment>();
#RequestMapping("/rest/equipment/{Number}")
public Equipment getEquipment(#PathVariable String Number){
if(!equiList.containsKey(Number)){
lNumber = DEFAULT;
}
return equiList.get(Number);
}
}
I'm writing the JUnit test case for the same as below:
import static org.springframework.test.web.ModelAndViewAssert.*;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration({/* include live config here
e.g. "file:web/WEB-INF/application-context.xml",
"file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {
#Inject
private ApplicationContext applicationContext;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private HandlerAdapter handlerAdapter;
private EquipmentController controller;
#Before
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
// Get the controller from the context here
controller = new EquipmentController();
}
#Test
public void testgetEquipment() throws Exception {
request.getUriString()("lNumber");
final Equipment equip = handlerAdapter.handle(request, response,
controller);
assertViewName(equip, "view");
}
}
But am not sure if this test class is correct or not as I am new to JUnit.
Can anyone please suggest how to do this.
Create a mock of your controller and use MockMvc to test your methods:
import static org.springframework.test.web.ModelAndViewAssert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration({/* include live config here
e.g. "file:web/WEB-INF/application-context.xml",
"file:web/WEB-INF/dispatcher-servlet.xml" */})
public class EquipmentControllerTest {
private MockMvc mockMvc;
private EquipmentController controller;
#Before
public void setUp() {
this.mockMvc = MockMvcBuilders.standaloneSetup(equipmentController).build()
}
#Test
public void testgetEquipment() throws Exception {
this.mockMvc.perform(get("/rest/equipment/{Number}", 3))
.andExpect(status().isOk())
}
}
where "3" represents value of your path variable.

Categories

Resources