英文:
Always return NullPointerException in array list when mocking in controller
问题
以下是翻译好的内容:
我尝试在控制器中使用JUnit和Mockito创建简单的单元测试。我模拟了服务,因为它将被控制器调用。
以下是代码片段:
@RunWith(MockitoJUnitRunner.class)
class MsCustomerControllerTest {
@Mock
MsCustomerService customerServiceMock;
@InjectMocks
MsCustomerController customerController;
@Test
void test() {
fail("Not yet implemented");
}
@Test
public void findAllCustomerTest() {
List<MsCustomer> listCustomer = new ArrayList<MsCustomer>();
listCustomer.add(new MsCustomer(1, "Rosa", "Titian Indah", LocalDateTime.now()));
listCustomer.add(new MsCustomer(2, "Rosa2", "Titian Indah2", LocalDateTime.now()));
when(customerServiceMock.findAllCustomer()).thenReturn(listCustomer);
ResponseEntity response = new ResponseEntity(listCustomer, HttpStatus.OK);
assertEquals(response, customerController.findAllCustomer());
}
}
注意:customerController还返回ResponseEntity,因此也要使用ResponseEntity进行断言。
以下是结果:
[![enter image description here][1]][1]
我尝试了其他方法,它也给了我NullPointerException。
请注意,我已经删除了代码部分,并只返回了您要求的翻译。如果您需要进一步的帮助,请随时提问。
英文:
I try to make simple unit test using JUnit and Mockito in the controller. I mocked the service because it will be called by the controller.
Here is the code
@RunWith(MockitoJUnitRunner.class)
class MsCustomerControllerTest {
@Mock
MsCustomerService customerServiceMock;
@InjectMocks
MsCustomerController customerController;
@Test
void test() {
fail("Not yet implemented");
}
@Test
public void findAllCustomerTest() {
List<MsCustomer> listCustomer = new ArrayList<MsCustomer>();
listCustomer.add(new MsCustomer(1, "Rosa", "Titian Indah", LocalDateTime.now()));
listCustomer.add(new MsCustomer(2, "Rosa2", "Titian Indah2", LocalDateTime.now()));
when(customerServiceMock.findAllCustomer()).thenReturn(listCustomer);
ResponseEntity response = new ResponseEntity(listCustomer, HttpStatus.OK);
assertEquals(response, customerController.findAllCustomer());
}
}
note: the customerController also return response entity, so assert with response entity too.
Here is the result
I have tried other method and it also give me the NullPointerException.
答案1
得分: 1
尝试添加一个初始化方法,并用@BeforeEach
进行注释。
然后在方法内部添加MockitoAnnotations.initMocks(this);
,它会初始化带有Mockito注释的字段。
@RunWith(MockitoJUnitRunner.class)
class MsCustomerControllerTest {
@Mock
MsCustomerService customerServiceMock;
@InjectMocks
MsCustomerController customerController;
@BeforeEach
void initMock() {
MockitoAnnotations.initMocks(this);
}
@Test
void test() {
fail("Not yet implemented");
}
@Test
public void findAllCustomerTest() {
List<MsCustomer> listCustomer = new ArrayList<MsCustomer>();
listCustomer.add(new MsCustomer(1, "Rosa", "Titian Indah", LocalDateTime.now()));
listCustomer.add(new MsCustomer(2, "Rosa2", "Titian Indah2", LocalDateTime.now()));
when(customerServiceMock.findAllCustomer()).thenReturn(listCustomer);
ResponseEntity response = new ResponseEntity(listCustomer, HttpStatus.OK);
assertEquals(response, customerController.findAllCustomer());
}
}
英文:
Try adding an init method and annotate it with @BeforeEach
.
Then inside the method add MockitoAnnotations.initMocks(this);
which initializes fields annotated with Mockito annotations.
@RunWith(MockitoJUnitRunner.class)
class MsCustomerControllerTest {
@Mock
MsCustomerService customerServiceMock;
@InjectMocks
MsCustomerController customerController;
@BeforeEach
void initMock() {
MockitoAnnotations.initMocks(this);
}
@Test
void test() {
fail("Not yet implemented");
}
@Test
public void findAllCustomerTest() {
List<MsCustomer> listCustomer = new ArrayList<MsCustomer>();
listCustomer.add(new MsCustomer(1, "Rosa", "Titian Indah", LocalDateTime.now()));
listCustomer.add(new MsCustomer(2, "Rosa2", "Titian Indah2", LocalDateTime.now()));
when(customerServiceMock.findAllCustomer()).thenReturn(listCustomer);
ResponseEntity response = new ResponseEntity(listCustomer, HttpStatus.OK);
assertEquals(response, customerController.findAllCustomer());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论