关于如何在JUnit和Spring中测试void返回方法的最佳实践?

发布时间:2020-07-07 17:07

我不确定我是否以正确的方式测试void的返回方法,也不确定我的class-under-test(cut)是否需要进行任何更改才能使其100%可测试和防虫。

由于未设置NullPointerException,执行test时看到loginOperations

错误:

java.lang.NullPointerException
    at com.demo.service.LoginService.doLogin(LoginService.java:40)
    at com.demo.service.LoginServiceTest.doLogin(LoginServiceTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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.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:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

LoginService.java

@Service
public class LoginService {

    @Autowired
    private ILoginOperations loginOperations;

    public void doLogin(HttpServletRequest request, String result) {
        LoginDTO loginDTO = new LoginDTO(request.getParameter("username"), result);
        loginOperations.doLogin(loginDTO);
    }
    
}

LoginServiceTest.java

public class LoginServiceTest {

    private LoginService instance = new LoginService();

    ILoginOperations loginOperations = Mockito.mock(ILoginOperations.class);
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    String result = "some string";

    @Test
    public void doLogin() {
        when(request.getParameter("username")).thenReturn("johndoe");
        instance.doLogin(request, result); //throws NPE while calling loginOperations.doLogin() because 
        assertNotNull(instance); //IS THIS THE CORRECT WAY TO TEST A VOID RETURNING METHOD ???
    }

}

现在,有两种方法可以修复测试。

  1. 我可以通过为setter类添加loginOperations方法并在setter中调用该test方法来修复被测类。
  2. @Test public void doLogin() {更改为@Test(expected = Exception.class) public void doLogin() {

不确定上面的最佳实践是什么,为什么?

另一个问题:

我遇到的另一个问题是如何在不返回任何内容的方法上assert。有类似verify()的东西,但不确定如何使用。

回答1

实际上,您应该为获取ILoginOperations的LoginService创建一个构造函数,通过这种方式,您可以在测试类中创建LoginService并将模拟的ILoginOperations传递为参数,所有这些工作都应该在@Before方法中完成。 / p>

或者您可以尝试将@InjectMocks用于LoginService,并将ILoginOperations注释为@Mock。

回答2

1。您可以通过在LoginService中添加setter方法来修复测试用例,也可以使用--p的构造函数注入

@Autowired 
public LoginService(ILoginOperations loginOperations) {
    this.loginOperations = loginOperations;
}
  1. 将异常验证为@Test(expected = Exception.class) public void doLogin()当然不是一个好主意,因为doLogin方法在正常情况下不会引发异常。

测试带有无效返回类型的方法的更好方法是使用验证API(例如-mockito verification API example)。您还可以使用Mockito的ArgumentCaptor捕获参数并声明该参数的状态,并将验证API用作-

@RunWith(MockitoJUnitRunner.class)
public class LoginServiceTest {

    @Captor
    private ArgumentCaptor<LoginDTO> captor;
    @Mock
    private ILoginOperations loginOperations;
    @Mock
    private HttpServletRequest mockServletRequest;
    @InjectMocks
    private LoginService loginService;

    @Test
    public void validateLogin() {
        when(mockServletRequest.getParameter("username")).thenReturn("mock_user_name");
        loginService.doLogin(mockServletRequest, "mock_result");

        verify(loginOperations).doLogin(captor.capture());
        LoginDTO expectedLoginDTO = captor.getValue();
        
        assertThat(expectedLoginDTO.getResult(), is("mock_result"));
        assertThat(expectedLoginDTO.getUsername(), is("mock_user_name"));
    }
}

马丁·福勒(Martin Fowler)有一篇很棒的文章介绍了这种测试方法-Mocks Aren't Stubs