So, i'm trying to create a unit test since its my first time doing this and here is the code:
package com.convergeict.shoppingcartservice.controller;
import com.convergeict.shoppingcartservice.model.CartDetails.CartSummary;
import com.convergeict.shoppingcartservice.model.CartDetails.ChargeSummary;
import com.convergeict.shoppingcartservice.model.CartDetails.MapOfCartItem;
import com.convergeict.shoppingcartservice.response.GenericResponse;
import com.convergeict.shoppingcartservice.service.CartDetailsService;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.ResponseEntity;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class CartDetailsControllerTest {
@InjectMocks
private CartDetailsController cartDetailsController;
@Mock
private CartDetailsService cartDetailsService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testGetCartDetailsSuccess() {
String cartInstanceIdentifier = "937f4199-ef42-470a-8a35-719c39a3c064";
CartSummary cartSummary = CartSummary.builder()
.statusCode("0000")
.statusDesc("Success")
.totalChargeSummary(new ChargeSummary())
.cartItemSummaryList(new ArrayList<>())
.build();
ResponseEntity<GenericResponse<?>> actualResponse = cartDetailsController.getCartDetails(cartInstanceIdentifier);
System.out.println(actualResponse);
assertNotNull(actualResponse);
}
}
here is the CartDetailsController class:
@RestController
@RequestMapping("/api")
public class CartDetailsController {
@Autowired
private CartDetailsService cartDetailsService;
@RequestMapping(value = "/v1/cart-details", method = RequestMethod.GET)
public ResponseEntity<GenericResponse<?>> getCartDetails(@RequestParam String cartInstanceIdentifier) {
return cartDetailsService.getCartDetails(cartInstanceIdentifier);
}
}
Upon running the testGetCartDetailsSuccess test, it's throwing me this exception:
java.lang.NullPointerException: Cannot invoke "com.convergeict.shoppingcartservice.service.CartDetailsService.getCartDetails(String)" because "this.cartDetailsService" is null
at com.convergeict.shoppingcartservice.controller.CartDetailsController.getCartDetails(CartDetailsController.java:21)
at com.convergeict.shoppingcartservice.controller.CartDetailsControllerTest.testGetCartDetailsSuccess(CartDetailsControllerTest.java:44)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
I have exhausted my options and can't seem to debug/fix this properly, I need help thanks.
I was expecting the actual response to return an actual response when hitting the actual API so I can assert it to a not null.