在控制器中模拟时始终返回数组列表中的空指针异常。

huangapple go评论78阅读模式
英文:

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(&quot;Not yet implemented&quot;);
	}
	

	
	@Test
	public void findAllCustomerTest() {
		
		List&lt;MsCustomer&gt; listCustomer = new ArrayList&lt;MsCustomer&gt;();
		listCustomer.add(new MsCustomer(1, &quot;Rosa&quot;, &quot;Titian Indah&quot;, LocalDateTime.now()));
		listCustomer.add(new MsCustomer(2, &quot;Rosa2&quot;, &quot;Titian Indah2&quot;, 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(&quot;Not yet implemented&quot;);
    }
    

    
    @Test
    public void findAllCustomerTest() {
        
        List&lt;MsCustomer&gt; listCustomer = new ArrayList&lt;MsCustomer&gt;();
        listCustomer.add(new MsCustomer(1, &quot;Rosa&quot;, &quot;Titian Indah&quot;, LocalDateTime.now()));
        listCustomer.add(new MsCustomer(2, &quot;Rosa2&quot;, &quot;Titian Indah2&quot;, LocalDateTime.now()));
        when(customerServiceMock.findAllCustomer()).thenReturn(listCustomer);
        
        ResponseEntity response = new ResponseEntity(listCustomer, HttpStatus.OK);
        
        
        assertEquals(response, customerController.findAllCustomer());
    }

}

huangapple
  • 本文由 发表于 2020年8月7日 12:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定