Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有可能在JUnit测试中引入方法响应吗?_Junit - Fatal编程技术网

有可能在JUnit测试中引入方法响应吗?

有可能在JUnit测试中引入方法响应吗?,junit,Junit,是否可以将底层方法的预期响应“馈送”到JUnit中测试的类?当然,您可以使用if-语句在代码处于“测试中”的情况下执行其他操作,或引入额外的对象包,将请求转发给对象(正常对象操作)或执行“其他操作”,但这会留下代码臭味和额外错误的感觉。有没有更好的方法让测试代码真正远离程序代码 我想象一下,如果classunderest.doSomething()调用ClassB.getValue(key)根本不调用该函数,相反,将值foo作为ClassB.getValue()的虚拟返回值返回到classund

是否可以将底层方法的预期响应“馈送”到JUnit中测试的类?当然,您可以使用
if
-语句在代码处于“测试中”的情况下执行其他操作,或引入额外的对象包,将请求转发给对象(正常对象操作)或执行“其他操作”,但这会留下代码臭味和额外错误的感觉。有没有更好的方法让测试代码真正远离程序代码


我想象一下,如果
classunderest.doSomething()
调用
ClassB.getValue(key)
根本不调用该函数,相反,将值
foo
作为
ClassB.getValue()
的虚拟返回值返回到
classunderest.doSomething()
?有办法吗?我可以稍后访问“key”来查看我的方法是否提出了正确的问题吗?(请参阅:我不想在这里测试
ClassB
的功能,只想测试'doSomething()̀方法本身。)

我认为您需要的是模拟框架

现在最受欢迎的似乎是