如何使用Mockito使用JUnit测试void方法

时间:2019-12-05 07:27:36

标签: java junit mockito

提前道歉-我知道这已经被问了一千遍了,但是我浏览了如此多的文章/文档,我迷路了。

我需要测试我的方法(无效)。我的项目已经完成,但是我需要测试。

OTPCommunicationServiceImpl.java

package com.example.smscommunication.service;

import com.example.smscommunication.constants.GatewayClient;
import com.example.smscommunication.dto.SmsRequest;
import com.example.smscommunication.gatewayclient.CommunicationGatewayFactory;
import com.example.smscommunication.gatewayclient.SmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

@Service
@Component
public class OTPCommunicationServiceImpl implements OTPCommunicationService {
/** In this file any information,error and trace print into console using "LOG" object */
 private static final Logger LOG = LoggerFactory.getLogger(OTPCommunicationServiceImpl.class);

/** Calling the CommunicationGatewayFactory */
@Autowired private CommunicationGatewayFactory communicationGatewayFactory;

/**
  * This method is used to iterator purpose on "Twilio and Nexmo".
  *
  * @param smsRequest Take the phone number into frontend. Take the value from
  *     communicationGatewayFactory.java based on for loop. After call the SendSms() ->
  *     SmsClient.java Interface return the value stored into the boolean. After check the result
  *     is "true" break the loop. If result is "false" again loop will be repeat.
  */
  @Override
  public void selectGatewayAndSendOtp(SmsRequest smsRequest){
   LOG.trace("--> selectGatewayAndSendOtp() -- : {}", smsRequest);

for (Map.Entry<Integer, SmsClient> entrySet :
    communicationGatewayFactory.getGatewayMap().entrySet()) {

  final boolean result = entrySet.getValue().sendSms(smsRequest);

  LOG.debug(
      "-- selectGatewayAndSendOtp() > OTP delivery attempt using {} client. Delivery Status : {}",
      GatewayClient.valueOf(entrySet.getKey()),
      result);
  if (result) break;
}
LOG.trace("<-- selectGatewayAndSendOtp()");
 }
 }

CommunicationGatewayFactory.java

package com.example.smscommunication.gatewayclient;

 import org.springframework.stereotype.Component;
 import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;

  @Component
  public class CommunicationGatewayFactory {

   @Resource private List<SmsClient> smsClientList;
    private Map<Integer, SmsClient> smsClientMap;

    /**
    * This method is used to store all the client implementation class in HashMap for communication
    * with gateway based on priority
    */
   @PostConstruct
   private void init() {
   smsClientMap = new HashMap<>();
   smsClientList.forEach(SmsClient -> smsClientMap.put(SmsClient.gatewayClient(), SmsClient));
    }
   /** @return a set view of the mappings contained in this map */
   public Map<Integer, SmsClient> getGatewayMap() {
   return smsClientMap;
   } 
    } 

0 个答案:

没有答案