mock

ScalaMock. Mock a class that takes arguments

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Full disclosure: I'm very new to mocking and mocking frameworks. I'm trying to use ScalaMock because it seemed like the 'default' mocking framework to use with ScalaTest but I am happy to use any other framework which is compatible with ScalaTest. The problem: I've written in Scala a class that talks to a socket. The class has a type parameter of what sort of socket it is to talk to and one of it's arguments is a factory for creating sockets of that type. It has the signature: class XScanner[T <: SocketClient]( confPath: String = "/etc

Mock final class with Mockito 2

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm removing Powermock from the project I'm currently working on, so I'm trying to rewrite some existing unitary test only with Mockito (mockito-core-2.2.28). When I run the test, I have the following error: org.mockito.exceptions.base.MockitoException: Cannot mock/spy class com.ExternalpackagePath.Externalclass Mockito cannot mock/spy because : final class I know that this question has already been asked ( How to mock a final class with mockito , Mock objects calling final classes static methods with Mockito ), but I didn't find the answer

Mock ServerVariables in the HttpContext.Current.Request

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: One of my services uses server variable provided by IIS through such code var value = System . Web . HttpContext . Current . Request . ServerVariables [ "MY_CUSTOM_VAR" ]; What I've tried is to mock those object and insert my own variable/collection and check few cases(e.g. variable is missing, value is null ...) I'm able to create instances of HttpContext, HttpRequest, HttpResponse and assign them properly however each of them is just a plain class without interface or virtual properties and initialization of ServerVariables

How to mock An Interface Java PowerMockito

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I m trying to mock an interface. public interface FlowCopyParamsBusinessManager { List < FlowCopyParams > findByAppli ( String application , String sourcePattern ) throws FlowCopyParamsBusinessException ; } In my code, when i call this method findByAppli , i would like to return a list of FlowCopyParams. List < FlowCopyParams > lstFlowCopyParams = flowCopyParamsBusinessManager . findByAppli ( "TOTO" , "TATA); Here my try in the class test: @BeforeClass public static void mockBeanIn () throws Exception { List < FlowCopyParams >

Enable Mock Locations in Android Marshmallow

匿名 (未验证) 提交于 2019-12-03 08:44:33
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I cannot get mock locations to work in Android Marshmallow. I have installed app in Android 6.0 . Now, When I want to set mock location for this app by following option. Setting >> Developer Option >> Select Mock location app I can not see any Application to select. Has anyone had success enabling mock locations under Marshmallow? 回答1: If you use the Mock location permission: <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> Then you will be able to select your app by Setting >> Developer Option >> Select Mock

Mocking a final method with PowerMock + EasyMock

匿名 (未验证) 提交于 2019-12-03 08:44:33
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm trying to mock a call to the final method ResourceBundle.getString() . With PowerMock 1.4.12 and EasyMock 3.1, the call is not being mocked; instead, the "real" method is called. My test class: @RunWith ( PowerMockRunner . class ) @PrepareForTest ( ResourceBundle . class ) public class TestSuite { @Before public void setUp () throws Exception { ResourceBundle resourceBundleMock = PowerMock . createNiceMock ( ResourceBundle . class ); expect ( resourceBundleMock . getString ( BundleConstants . QUEUE )). andReturn ( "Queue" );

How can I create a SerialPort mock using the Moq library?

匿名 (未验证) 提交于 2019-12-03 08:42:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have to write a lot of code that deals with serial ports. Usually there will be a device connected at the other end of the wire and I usually create my own mocks to simulate their behavior. I'm starting to look at Moq to help with my unit tests. It's pretty simple to use it when you need just a stub, but I want to know if it is possible and if yes how do I create a mock for a hardware device that responds differently according to what I want to test. A simple example: One of the devices I interface with receives a command (move to position

PowerMock: How to unmock a method?

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a static method that is mocked using PowerMock to throw an exception. (It deletes files.) Unfortunately, during my @After (after-each-test) method, I need to call this method without the mocks. How can I umock a method? I don't see an equivalent to Mockito.reset() . [ Ref: mockito : how to unmock a method? ] Example: @RunWith(PowerMockRunner.class) @PrepareForTest(PathUtils.class) // Important: This class has a static method we want to mock. public class CleaningServiceImplTest2 extends TestBase { public static final File testDirPath

How to mock Amazon S3 in an integration test

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to get an "walking skeleton" of my app up that will use S3 for persistence. I would like to use a fake S3 service so each developer's desktop can read/write at will. I thought mocks3 would be perfect, as I could get a jetty server up in my jUnit tests. The problem is that mocks3 doesn't allow any writes . Not even to set it up as far as I can tell. So how do others do this? 回答1: Tornado, a python web framework, has an example app that is just what you're looking for. https://github.com/facebook/tornado/blob/master/demos/s3server

Mock HttpContext for unit testing a .NET core MVC controller?

匿名 (未验证) 提交于 2019-12-03 08:28:06
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a function in a controller that I am unit testing that expects values in the header of the http request. I can't initialize the HttpContext because it is readonly. My controller function expects a http request header value for "device-id" [TestMethod] public void TestValuesController() { ValuesController controller = new ValuesController(); //not valid controller.HttpContext is readonly //controller.HttpContext = new DefaultHttpContext(); var result = controller.Get(); Assert.AreEqual(result.Count(), 2); } Is there a straight-forward