草庐IT

mocking-stubbing

全部标签

java - Mockito - stub 由模拟对象方法返回的对象的方法

假设我有一个模拟对象,我不想stub它的任何方法,但我想stub它返回的对象的方法。例如,when(mockObject.method1()).thenReturn(returnValue)这是通常的做法,但我正在寻找,when(mockObject.method1().method2()).thenReturn(returnValue)这可能吗?如果我这样做,我会得到一个NullPointerException。目前我stub第一个方法来返回模拟对象,然后使用返回的模拟对象stub第二个方法。但是,这些临时模拟对象对我来说毫无用处,在将许多方法链接在一起之后,会产生很多无用的模拟对象

java - Mockito - "Wanted but not invoked; However there were other interactions with this mock"错误

我正在验证是否使用Mockito调用了一个函数,但Mockito告诉我我正在验证的函数从未被调用过,并且调用了其他函数。但在我看来,我正在调用正确的函数......这是我遇到的错误的堆栈跟踪:Wantedbutnotinvoked:relationshipAutoIndexer.getAutoIndex();->atorg.whispercomm.manes.server.graph.DataServiceImplTest.testInitIndices(DataServiceImplTest.java:117)However,therewereotherinteractionswit

java - mockito:有没有办法捕获 stub 方法的返回值?

如果我模拟一个方法来返回某个对象的新实例,我如何捕获返回的实例?例如:when(mock.someMethod(anyString())).thenAnswer(newAnswer(){Objectanswer(InvocationOnMockinvocation){Object[]args=invocation.getArguments();Objectmock=invocation.getMock();returnnewFoo(args[0])}});显然,我可以有一个Foo类型的字段,并在answer中将其设置为新实例,但是有更好的方法吗?像ArgumentCaptor这样的东西

java - Mockito Spy - 在调用构造函数之前 stub

我正在尝试监视一个对象,我想在构造函数调用它之前stub一个由构造函数调用的方法。我的类(class)看起来像这样:publicclassMyClass{publicMyClass(){setup();}publicvoidsetup(){}}不得调用设置方法。那么,我该如何监视这个方法(和stub设置以使其不执行任何操作)?它可以很好地模拟方法,但我想对MyClass进行单元测试,所以我需要非常其他的方法。之所以需要对setup方法进行stub处理,使其什么都不做:我正在为乐高机器人(lejos)编程,并在机器人需要工作的设置中放置了一些代码。但是,当我在TinyVM(安装在机器人上

java - PowerMock:来自父类的 stub 方法

我正在使用PowerMock,我想知道如何保留子类的所有行为,但stub可能被子类覆盖的super调用。假设我有这个类:publicclassA{publicStringsomeMethod(){return"Idon'twanttoseethisvalue";}}和一个子类:publicclassBextendsA{@OverridepublicStringsomeMethod(){returnsuper.someMethod()+",butIwanttoseethisone";}}如何stub调用super.someMethod()?我试过了@Testpublicvoidtest(

Python Mock - 模拟几个打开

阅读此文后:HowdoImockanopenusedinawithstatement(usingtheMockframeworkinPython)?我可以使用以下方法在python中模拟open函数:withpatch(open_name,create=True)asmock_open:mock_open.return_value=MagicMock(spec=file)m_file=mock_open.return_value.__enter__.return_valuem_file.read.return_value='text1'diffman=Diffman()diffman.

python - 无法使用 Python 的 mock.patch 模拟 urllib2.urlopen

下面是我的api.py模块的代码片段#-*-coding:utf-8-*-fromurllib2importurlopenfromurllib2importRequestclassAPI:defcall_api(self,url,post_data=None,header=None):is_post_request=Trueif(post_dataandheader)elseFalseresponse=Nonetry:ifis_post_request:url=Request(url=url,data=post_data,headers=header)#Callingapiapi_re

python - mock_s3 装饰 pytest fixture

我想知道为什么mock_s3装饰器在用作pytestfixture的装饰器时不起作用。test_with_fixture失败,而它提供与test_withoutfixture相同的代码。好吧,“相同”,因为它被明确装饰了。test_with_fixture引发了AccessDenied错误,但在这种情况下它与S3错误的类型无关。问题是,client.list_objects在使用fixture的测试中没有被模拟。pytest-3.1.2moto-1.0.1boto3-1.0.4importpytestimportboto3frommotoimportmock_s3BUCKET='Foo

python - Pytest 适用于旧模拟,但不适用于 unittest.mock

我正在将一些代码从Python2移植到3,而py.test不能很好地与来自unittest.mockpatch装饰器一起玩。当我使用patch装饰器将模拟传递给测试函数的参数时,py.test反而将该参数解释为固定装置,并且无法设置测试。这里有一个人为的例子,希望能说明问题:@patch('my_module.my_func')deftest_my_func(mock_func):mock_func()mock_func.assert_called_once_with()运行py.test后,错误消息如下所示:Efixture'my_func'notfound>availablefix

python - mock.call_count 的线程安全版本

看起来Mock.call_count不能与线程一起正常工作。例如:importthreadingimporttimefrommockimportMagicMockdeff():time.sleep(0.1)deftest_1():mock=MagicMock(side_effect=f)nb_threads=100000threads=[]for_inrange(nb_threads):thread=threading.Thread(target=mock)threads.append(thread)thread.start()forthreadinthreads:thread.joi