对带有String和Class <T>参数的方法使用mokito.when

问题描述

我有一种方法,我想创建一些单元测试(此方法会翘曲

applicationContext.getEnvironment()。getProperty(key,class)

用于领事集成)

长话短说:

from flask import Flask,request,redirect
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route("/sms",methods=['GET','POST'])
def incoming_sms():
    """Send a dynamic reply to an incoming text message"""
    # Get the message the user sent our Twilio number
    body = request.values.get('Body',None)

    # Start our TwiML response
    resp = MessagingResponse()

    # Determine the right reply for this message
    if body == 'hello':
        resp.message("Hi!")
    elif body == 'bye':
        resp.message("Goodbye")

    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)

在使用DynamicProperties类测试其他某些类时,如下所示:

public class DynamicProperties {

 public <T> T getEnvironmentProperty(String key,Class<T> cls) {
        return cls.cast(applicationContext.getEnvironment().getProperty(key,cls));
    }
}

KEY_A KEY_B是公共静态最终字符串 我收到以下错误:

   @Test
    void testA() {
        //before
        when(dynamicProperties.getEnvironmentProperty(eq(KEY_A),Boolean.class)).thenReturn(true);
        when(dynamicProperties.getEnvironmentProperty(eq(KEY_B),Long.class)).thenReturn(0l);

        //when
        archivedSensorsService.testMethod();

        //than
        verify(...)
    }

尝试以下操作时:

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(),"raw String");
When using matchers,all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(),eq("String by matcher"));

出现以下错误:

 @Test
        void testA() {
            //before
            when(dynamicProperties.getEnvironmentProperty(eq(KEY_A),anyObject())).thenReturn(true);
            when(dynamicProperties.getEnvironmentProperty(eq(KEY_B),anyObject())).thenReturn(0l);
    
            //when
            archivedSensorsService.testMethod();
    
            //than
            verify(...)
        }

有什么建议吗?

解决方法

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

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

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