Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用存根、验证和模拟(Mockito)的OOP单元测试:白盒方法_Java_Unit Testing_Oop_Junit - Fatal编程技术网

Java 使用存根、验证和模拟(Mockito)的OOP单元测试:白盒方法

Java 使用存根、验证和模拟(Mockito)的OOP单元测试:白盒方法,java,unit-testing,oop,junit,Java,Unit Testing,Oop,Junit,我正在学习使用存根进行OOP单元测试,使用白盒方法进行验证和模拟。 我对他们的了解是: 存根提供假输入(或获取假输入?) mock创建该类的对象 验证验证输出是否正确完成 根据这一点,我有一个例子(伪代码)如下: 它是一个子模块changeAddress,在其中它分别从用户,对话框,验证器类导入对象inUser,inDialogBox Submodule changeAddress Imports: inUser (User), inDialogBox (DialogBox), inVali

我正在学习使用存根进行OOP单元测试,使用白盒方法进行验证和模拟。
我对他们的了解是:

  • 存根提供假输入(或获取假输入?)
  • mock创建该类的对象
  • 验证验证输出是否正确完成
根据这一点,我有一个例子(伪代码)如下:

它是一个子模块
changeAddress
,在其中它分别从
用户
对话框
验证器
类导入对象
inUser
inDialogBox

Submodule changeAddress
Imports: inUser (User), inDialogBox (DialogBox), inValidator (Validator)
Exports: nothing
Algorithm:

IF inUser.loggedIn THEN
    newAddress = inDialogBox.getAddress
    IF inValidator.validAddress <-- newAddress THEN
        inUser.setAddress <-- newAddress
    ELSE
        inDialogBox.message <-- "Invalid address"
    ENDIF
ElSE
   inDialogBox.message <-- "Not logged in"
ENDIF

我不确定我是否走上了正确的道路

在本例中,我假设要测试的方法(changeAddress)是在类ClassToTest中定义的

ClassToTest在changeAddress()中使用了三个对象:目标是独立于实现进行测试 在这三个对象中:这就是我们嘲笑它们的原因

下面是一个如何做到这一点的示例:

public class MyTest {
    private User inUser;
    private DialogBox inDialogBox;
    private Validator inValidator;
    private ClassToTest classToTest;

    @Before
    public void setup() {
        inUser = mock(User.class);
        inDialogBox = mock(DialogBox.class);
        inValidator = mock(Validator.class);

        classToTest = new ClassToTest(inUser, inDialogBox, inValidator);
    }

    @Test
    //1st test case when loggedin() is true aswell as the validAddress() is true
    public void testChangeAddressWithValidAddress() {
        when(inUser.loggedIn()).thenReturn(true);     // STUB
        when(inDialogBox.getAddress()).thenReturn("Test address");  // STUB
        when(inUser.setAddress("Test address")).thenReturn(true);   // STUB
        classToTest.changeAddress(inUser, inDialogBox, inValidator);          
        verify(inUser).setAddress("Test address");         //VERIFY
    }

    @Test
    //2nd test case when loggedin()is false
    public void testChangeAddressWhenInvalidLogin() {
        when(inUser.loggedIn()).thenReturn(false);      // STUB
        classToTest.changeAddress(inUser, inDialogBox, inValidator); 
        verify(inDialogBox).message("Not logged in");   //VERIFY
    }

    @Test
    //3rd test case when loggedin() is true but validAddress() is false
    public void testChangeAddressWhenValidLoginAndInvalidAddress() {
        when(inUser.loggedIn()).thenReturn(true);    // STUB                    
        when(inDialogBox.getAddress()).thenReturn("Test address");      // STUB
        when(inUser.setAddress("Test address")).thenReturn(false);      // STUB
        classToTest.changeAddress(inUser, inDialogBox, inValidator);
        verify(inDalogBox).message("Invalid address");      //VERIFY
    }
}

我认为,您走的是正确的道路:您至少需要3个测试,并且必须模拟您的被测类正在使用的所有外部服务。我建议为每个测试用例创建一个不同的方法(这意味着3种方法)。还要将您的mock声明为类的私有成员,并在setup()方法中对其进行初始化,并用@Before.注释。您能否通过回答清楚地说明这一点?因为我不太理解:/I我理解setup()方法,用@before注释,但我不太明白,如何将其拆分为不同的方法?任何提示/答案都会非常有用!我在下面举了一个例子。。。
public class MyTest {
    private User inUser;
    private DialogBox inDialogBox;
    private Validator inValidator;
    private ClassToTest classToTest;

    @Before
    public void setup() {
        inUser = mock(User.class);
        inDialogBox = mock(DialogBox.class);
        inValidator = mock(Validator.class);

        classToTest = new ClassToTest(inUser, inDialogBox, inValidator);
    }

    @Test
    //1st test case when loggedin() is true aswell as the validAddress() is true
    public void testChangeAddressWithValidAddress() {
        when(inUser.loggedIn()).thenReturn(true);     // STUB
        when(inDialogBox.getAddress()).thenReturn("Test address");  // STUB
        when(inUser.setAddress("Test address")).thenReturn(true);   // STUB
        classToTest.changeAddress(inUser, inDialogBox, inValidator);          
        verify(inUser).setAddress("Test address");         //VERIFY
    }

    @Test
    //2nd test case when loggedin()is false
    public void testChangeAddressWhenInvalidLogin() {
        when(inUser.loggedIn()).thenReturn(false);      // STUB
        classToTest.changeAddress(inUser, inDialogBox, inValidator); 
        verify(inDialogBox).message("Not logged in");   //VERIFY
    }

    @Test
    //3rd test case when loggedin() is true but validAddress() is false
    public void testChangeAddressWhenValidLoginAndInvalidAddress() {
        when(inUser.loggedIn()).thenReturn(true);    // STUB                    
        when(inDialogBox.getAddress()).thenReturn("Test address");      // STUB
        when(inUser.setAddress("Test address")).thenReturn(false);      // STUB
        classToTest.changeAddress(inUser, inDialogBox, inValidator);
        verify(inDalogBox).message("Invalid address");      //VERIFY
    }
}