Mockito Java:如何测试调用其他类来检索值的方法

时间:2016-03-11 19:56:15

标签: java mockito

我正在尝试测试一个方法初始化事务,该方法创建一个唯一的事务Id。此方法使用其他类的引用来检索属性。

在模拟引用类之后,我仍然得到空指针异常。当我尝试测试。以下是我的代码。

注意:JMockito

任何帮助表示赞赏

  public ResponseDto initializeTransaction(RequestDTO request){

    try {

        String transactionId =getTransactionId(request);
        ResponseDTO result = new ResponseDTO();
        result.setTransactionId(transactionId);
        return result;
        }

}

public String getTransactionId(CreditCardGwtInitializeRequestDTO request){

    StringBuffer transactionId = new StringBuffer();
    String customerId = customerIdentifier.getCustomer();
    UserDto userDto = user.getUserDetails(request.getKey());
    String userWorkStationId =userDto.getWorkStationId();
    transactionId.append(String.valueOf(System.currentTimeMillis()) + "-"); 
    transactionId.append(userDto.getObjId()+ "-");      
    transactionId.append(transactionIdEncode.encode(customerId));
    transactionId.append("-");
    transactionId.append(transactionIdEncode.encode(userWorkStationId));    
    return transactionId.toString();    

}

测试课

public class CreditCardGwtInitializeServiceImplTest {
private CreditCardGwtInitializeServiceImpl test;
@Mock
 private CustomerIdentifier customerIdentifier;
@Mock
private UserDto userDto;
@Mock
private UserDetails user;
private CreditCardGwtInitializeRequestDTO request;
@Mock
TransactionIdCharacterEncoding transactionId;
@Before
public void setup() {
    MockitoAnnotations.initMocks(this); 
}
@Test
public void when_profilename_notNull_retrieveByName() throws Exception {
    //test.setUser(user);
    CreditCardGwtInitializeResponseDTO expected = new CreditCardGwtInitializeResponseDTO();
    expected.setGatewayPassKey("");
    String profileName="theName";
    String connectionKey ="123456";
    String custId ="custId";

    request.setGatewayProfile(profileName);
    request.setConnectionKey(connectionKey);
    //userDto.setWorkStationId("12345");
    //userDto.setObjId(12345L);


    when(customerIdentifier.getCustomer()).thenReturn(custId);

    when(user.getUserDetails(anyString())).thenReturn(userDto);

    when(userDto.getWorkStationId()).thenReturn("RTYTYU");

    when(userDto.getObjId()).thenReturn(1232324L);


    when(transactionId.encode(anyString())).thenReturn("01010101");



    CreditCardGwtInitializeResponseDTO response = test.initializeTransaction(request);  
    assertEquals(expected,response );

    verifyZeroInteractions(gatewayProfileRetrievalService);
}

2 个答案:

答案 0 :(得分:0)

通过添加线条解决了我的问题

   MockitoAnnotations.initMocks(this);
    test = new CreditCardGwtInitializeServiceImpl();
    test.setUser(user);
    test.setCustomerIdentifier(customerIdentifier);
    test.setTransactionIdEncode(transactionId);

答案 1 :(得分:0)

以下是使用Mockito魔术而非手动初始化的替代解决方案(替换setup方法)。

首先使用mockito提供的默认JUnit Runner类覆盖默认的JUnit Runner类(这与MockitoAnnotations.initMocks(this)具有相同的效果):

@RunWith(MockitoJUnitRunner.class)
public class CreditCardGwtInitializeServiceImplTest {
...

然后在声明它之后立即实例化你的对象并让mockito处理注入(这与你的解决方案的5行具有相同的效果):

@InjectMocks
private CreditCardGwtInitializeServiceImpl test = new CreditCardGwtInitializeServiceImpl();
...