找到你要的答案

Q:How do I test the values passed as a parameter to a method inside another method (Java Unit Test)?

Q:我如何测试作为一个参数,在另一个方法传递的值(java单元测试)?

Example:

public class myClass {
private WebService webService;
public void process(String delimitedString) {
    String[] values = StringUtils.split(delimitedString, "$%");

    insideMethod.setFirstName(values[0]);
    insideMethod.setMiddleName(values[1]);
    insideMethod.setLastName(values[2]);
    insideMethod.setBirthDate(values[3]);

    webService.getResponseWS(insideMethod.getFirstName,
                             insideMethod.getMiddleName, 
                             insideMethod.getLastName, 
                             insideMethod.getBirthDate);
}
}

I want to test that the right values are being set in insideMethod to make sure that the correct parameters are being passed to webService.getResponseWS() This is in Java and I should use unit tests and Mokito.

Note: I am not testing the webService method. I need to test that the values passed to insideMethod are correct. Example "John" for name instead of "JohnSm" or "John$%".

So far I have created a test class, instantiated the class being tested and mocked the webService class.

public class TestClass {
        MyClass myClass = new MyClass();
        private WebService webService = mock(WebService.class);
  public void processTest() {
  when(webService.getResponseWS()).thenCallRealMethod();
    insideMethod.process("John$%J$%Smith$%02-02-1990");

例子:

public class myClass {
private WebService webService;
public void process(String delimitedString) {
    String[] values = StringUtils.split(delimitedString, "$%");

    insideMethod.setFirstName(values[0]);
    insideMethod.setMiddleName(values[1]);
    insideMethod.setLastName(values[2]);
    insideMethod.setBirthDate(values[3]);

    webService.getResponseWS(insideMethod.getFirstName,
                             insideMethod.getMiddleName, 
                             insideMethod.getLastName, 
                             insideMethod.getBirthDate);
}
}

I want to test that the right values are being set in insideMethod to make sure that the correct parameters are being passed to webService.getResponseWS() This is in Java and I should use unit tests and Mokito.

注:我不是测试Web服务的方法。我需要测试通过insidemethod的值是正确的。例如“约翰”的名字,而不是“johnsm”或“约翰$ %”。

到目前为止,我已经创建了一个测试类,实例化测试和嘲笑的WebService的类。

public class TestClass {
        MyClass myClass = new MyClass();
        private WebService webService = mock(WebService.class);
  public void processTest() {
  when(webService.getResponseWS()).thenCallRealMethod();
    insideMethod.process("John$%J$%Smith$%02-02-1990");
answer1: 回答1:

You want to use Mockito.verify().

The JavaDoc aor the Mockito Verify Cookbook lists a lot of examples.

你想用mockito。verify()。

Javadoc AOR mockito食谱中列举了大量实例验证。

answer2: 回答2:
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class TestClass {

    @Mock
    private WebService webService; 

    private MyClass myClass = new MyClass();

    @Test
    public void processTest() {
        // inject webService mocked instance into myClass instance, either with a setter
        myClass.setWebService(webService);
        // or using Mockito's reflection utils if no setter is available
        //Whitebox.setInternalState(myClass, "webService", webService);

        // call the method to be tested
        String input = "input"; // whatever your input should be for the test
        myClass.process(input);

        // verify webService behavior
        verify(webService).getResponseWs(
                "expectedInput1", "expectedInput2", "expectedInput3", "expectedInput4");
    }

}
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class TestClass {

    @Mock
    private WebService webService; 

    private MyClass myClass = new MyClass();

    @Test
    public void processTest() {
        // inject webService mocked instance into myClass instance, either with a setter
        myClass.setWebService(webService);
        // or using Mockito's reflection utils if no setter is available
        //Whitebox.setInternalState(myClass, "webService", webService);

        // call the method to be tested
        String input = "input"; // whatever your input should be for the test
        myClass.process(input);

        // verify webService behavior
        verify(webService).getResponseWs(
                "expectedInput1", "expectedInput2", "expectedInput3", "expectedInput4");
    }

}
answer3: 回答3:

Provided you've set up Mockito and injected your mocks correctly, the following should work (although I don't have a compiler or JVM at hand to check).

verify(webService).getResponseWS("John", "J", "Smith", "02-02-1990");

如果你已经建立了mockito注入你的嘲笑是正确的,应该这样(虽然我没有编译器或JVM在手检查)。

verify(webService).getResponseWS("John", "J", "Smith", "02-02-1990");
java  unit-testing  mockito