Unitest使用mockito(一个类依赖于其他场景)

时间:2022-05-18 14:13:54

Hi I am very new to mocking framework. Can any one please help me to write a junit by using any mocking framework. Below is my requirement

嗨,我是嘲弄框架的新手。任何人都可以帮助我使用任何模拟框架编写junit。以下是我的要求

I wanted to write a unitest for getListOfEmp method by using mock with expected value.

我想通过使用带有期望值的mock为getListOfEmp方法编写一个unitest。

Note Here I am facing difficulty to mock EmpValue class to get the accurate value in ServiceClass

注意这里我很难模拟EmpValue类来获取ServiceClass中的准确值

 public class ServiceClass {
    public Employe getListOfEmp(List<String> criteria) {
    Employe emp = new Employe();
    EmpValue empValue = new EmpValue();

    if (criteria.contains("IT")) {
        emp.setEid(empValue.getIt());
    }
    if (criteria.contains("SALES")) {
        emp.setEid(empValue.getSales());
    }
    if (criteria.contains("SERVICE")) {
        emp.setEid(empValue.getService());
    }
    return emp;
   }
}


public class EmpValue {
    private String it = "IT-1001";
    private String service = "SERVICE-1001";
    private String sales = "SALES-1001";
    public String getIt() {
       return it;
    }
    public String getService() {
        return service;
    } 
    public String getSales() {
       return sales;
    }
  }

1 个解决方案

#1


First I will do some changes to the code to make it testable.

首先,我将对代码进行一些更改以使其可测试。

1: EmplValue object should be passed to the method by the client or should be an instance variable in your Service class. Here I am using it as an instance variable, so that client can set it.

1:EmplValue对象应该由客户端传递给方法,或者应该是Service类中的实例变量。这里我将它用作实例变量,以便客户端可以设置它。

public class ServiceClass {
    private EmpValue empValue;

    public ServiceClass(EmpValue empValue){
        this.empValue = empValue;
    }

    public Employe getListOfEmp(List<String> criteria) {
        Employe emp = new Employe();

        if (criteria.contains("IT")) {
            emp.setEid(empValue.getIt());
        }
        if (criteria.contains("SALES")) {
            emp.setEid(empValue.getSales());
        }
        if (criteria.contains("SERVICE")) {
            emp.setEid(empValue.getService());
        }
        return emp;
    }
}

I am using Mockito and JUnit to write unit-test for this class .

我正在使用Mockito和JUnit为这个类编写单元测试。

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

@RunsWith(MockitoJunitRunner.class)
public class ServiceClassTest{

@Test  
public void shouldReturnEmployeeWithEidSetWhenCriteriaIsIT(){
   // Craete mock and define its behaviour
   EmpValue mEmpValue = mock(EmpValue.class);
   when(mEmpValue.getIt()).thenReturn("IT-1001");

  // Create the object of class user test with mock agent and invoke
   ServiceClass serviceClass = new ServiceClass(mEnpValue);
   Employee result = serviceClass.getListOfEmp(asList("IT"));

   // Verify the result as per your expectation
   assertEquals("It-001", result.getEid());
}

}

#1


First I will do some changes to the code to make it testable.

首先,我将对代码进行一些更改以使其可测试。

1: EmplValue object should be passed to the method by the client or should be an instance variable in your Service class. Here I am using it as an instance variable, so that client can set it.

1:EmplValue对象应该由客户端传递给方法,或者应该是Service类中的实例变量。这里我将它用作实例变量,以便客户端可以设置它。

public class ServiceClass {
    private EmpValue empValue;

    public ServiceClass(EmpValue empValue){
        this.empValue = empValue;
    }

    public Employe getListOfEmp(List<String> criteria) {
        Employe emp = new Employe();

        if (criteria.contains("IT")) {
            emp.setEid(empValue.getIt());
        }
        if (criteria.contains("SALES")) {
            emp.setEid(empValue.getSales());
        }
        if (criteria.contains("SERVICE")) {
            emp.setEid(empValue.getService());
        }
        return emp;
    }
}

I am using Mockito and JUnit to write unit-test for this class .

我正在使用Mockito和JUnit为这个类编写单元测试。

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

@RunsWith(MockitoJunitRunner.class)
public class ServiceClassTest{

@Test  
public void shouldReturnEmployeeWithEidSetWhenCriteriaIsIT(){
   // Craete mock and define its behaviour
   EmpValue mEmpValue = mock(EmpValue.class);
   when(mEmpValue.getIt()).thenReturn("IT-1001");

  // Create the object of class user test with mock agent and invoke
   ServiceClass serviceClass = new ServiceClass(mEnpValue);
   Employee result = serviceClass.getListOfEmp(asList("IT"));

   // Verify the result as per your expectation
   assertEquals("It-001", result.getEid());
}

}