模拟单例实例并验证模拟失败

问题描述

我有一个实现类,它实现一个接口并覆盖其方法。实现类获取类的单例实例,需要在JUnit中对其进行模拟。我尝试使用PowerMock,但在这里无法正常使用。 另外,在运行此处提到的测试时,我得到此处提到的错误https://codeshare.io/5NyJyr

这是实现类:

@Slf4j
@Component
public class PhoneserviceImpl implements NotificationService {

    private String notificationServiceURL;
    private PhoneContext phoneContext;

    public PhoneserviceImpl(@Value("${notificationService.url}") String notificationServiceURL) {
        this.notificationServiceURL = notificationServiceURL;
    }

    @Override
    public void sendPhonesMSByEmployeeIds(String title,String message,List<String> employeeIds) {
        log.info("Sending notifications by Employee Id");

        SendNotificationByEmployeeIds sendNotificationByEmployeeIds = SendNotificationByEmployeeIds.getInstance();
        sendNotificationByEmployeeIds.setNotificationServiceURL(notificationServiceURL);
        phoneContext = new PhoneContext(sendNotificationByEmployeeIds);
        phoneContext.notify(title,message,employeeIds);
    }
}

测试:


public class Test {

    PhoneserviceImpl phoneserviceImpl;

    @Mock
    PhoneContext phoneContext;

    @Value("${phoneservice.url}")
    String phoneserviceUrl;

    @BeforeEach
    void setUp() {
        MockitoAnnotations.openMocks(this);
        phoneserviceImpl = new PhoneserviceImpl("some");
    }

    @org.junit.jupiter.api.Test
    void simpletest() {
        phoneserviceImpl.sendPhonesMSByEmployeeIds(anyString(),anyString(),anyList());
        verify(phoneContext,Mockito.times(1)).notify(anyString(),anyList());
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)