mock

Why am I getting an Exception with the message “Invalid setup on a non-virtual (overridable in VB) member…”?

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a unit test where I have to mock a non-virtual method that returns a bool type public class XmlCupboardAccess { public bool IsDataEntityInXmlCupboard(string dataId, out string nameInCupboard, out string refTypeInCupboard, string nameTemplate = null) { return IsDataEntityInXmlCupboard(_theDb, dataId, out nameInCupboard, out refTypeInCupboard, nameTemplate); } } So I have a mock object of XmlCupboardAccess class and I am trying to setup mock for this method in my test case as shown below [TestMethod] Public void Test() { private string

Setup async Task callback in Moq Framework

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got an interface which declares Task DoSomethingAsync(); I'm using MoqFramework for my tests: [TestMethod()] public async Task MyAsyncTest() { Mock mock = new Mock (); mock.Setup(arg => arg.DoSomethingAsync()).Callback(() => { }); ... } Then in my test I execute the code which invokes await DoSomethingAsync() . And the test just fails on that line. What am I doing wrong? 回答1: Your method doesn't have any callbacks so there is no reason to use .CallBack() . You can simply return a Task with the desired values using .Returns() and Task

How to mock a web server for unit testing in Java?

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I would like to create a unit test using a mock web server. Is there a web server written in Java which can be easily started and stopped from a JUnit test case? 回答1: Try Simple ( Maven ) its very easy to embed in a unit test. Take the RoundTripTest and examples such as the PostTest written with Simple. Provides an example of how to embed the server into your test case. Also Simple is much lighter and faster than Jetty, with no dependencies. So you won't have to add several jars on to your classpath. Nor will you have to be

PHPUnit: how do I mock multiple method calls with multiple arguments?

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am writing a unit test for a method using PHPUnit. The method I am testing makes a call to the same method on the same object 3 times but with different sets of arguments. My question is similar to the questions asked here and here The questions asked in the other posts have to do with mocking methods that only take one argument. However, my method takes multiple arguments and I need something like this: $mock->expects($this->exactly(3)) ->method('MyMockedMethod') ->with($this->logicalOr($this->equalTo($arg1, $arg2, arg3....argNb), $this-

How to mock an SqlDataReader using Moq - Update

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm new to moq and setting up mocks so i could do with a little help. How do I mock up an SqlDataReader using Moq? Update After further testing this is what I have so far: private IDataReader MockIDataReader() { var moq = new Mock<IDataReader>(); moq.Setup( x => x.Read() ).Returns( true ); moq.Setup( x => x.Read() ).Returns( false ); moq.SetupGet<object>( x => x["Char"] ).Returns( 'C' ); return moq.Object; } private class TestData { public char ValidChar { get; set; } } private TestData GetTestData() { var testData = new TestData(); using (

Monkey patching a class in another module in Python

匿名 (未验证) 提交于 2019-12-03 02:42:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm working with a module written by someone else. I'd like to monkey patch the __init__ method of a class defined in the module. The examples I have found showing how to do this have all assumed I'd be calling the class myself (e.g. Monkey-patch Python class ). However, this is not the case. In my case the class is initalised within a function in another module. See the (greatly simplified) example below: thirdpartymodule_a.py class SomeClass(object): def __init__(self): self.a = 42 def show(self): print self.a thirdpartymodule_b.py import

How to mock RestTemplet with MockRestServiceServer?

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: @RunWith(MockitoJUnitRunner.class) public class FeatureFlipperManagerTest { @Autowired RestTemplate restTemplate = new RestTemplate(); @Autowired Service service = new Service(); MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate); @Test public void test() throws Exception { mockServer.expect(requestTo(Mockito.anyString())) .andRespond(withSuccess("{\"enabled\":true}", MediaType.APPLICATION_JSON)); boolean res = service.isEnabled("xxx"); mockServer.verify(); Assert.assertEquals(true, res); } } I have

Powermock mocking void method throws error

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using Powermockito, mockito with TestNG. My test class extends PowerMockTestCase. I want to mock a void method. For that I used following sample syntax, @PrepareForTest(TestClass.class) class Sample extends PowerMockTestCase{ @BeforeClass public void beforeClass(){ TestClass obj = PowerMockito.spy(TestClass.getInstance()); PowerMockito.doNothing().when(obj).voidMethodName(Matchers.any(Type1.class), Matchers.anyString(), Matchers.any(Type2.class)); } When I give this, I get : FAILED CONFIGURATION: @BeforeClass beforeClass org.mockito

Unable to Mock Glassfish Jersey Client response object

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am having problems with creating a mock Response object to use with my unit tests. I am using org.glassfish.jersey.core.jersey-client version 2.3.1 to implement my RESTful client and mockito version 1.9.5 to help me with mock objects. Here is my test's code: @Test public void testGetAll () throws IOException { // Given String expectedResource = "expectedResource" final Response expectedRes = Response . ok ( expectedResource , MediaType . APPLICATION_JSON ). build (); String receivedResource ; BDDMockito . given ( this . client .

Mockito verify a function is invoked once in my case

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I am using Mockito to write my test case. I have a simple class which contains a function countPerson(boolean) which I am interested to test: public class School { //School is a singleton class. public void countPerson ( boolean includeTeacher ) { if ( includeTeacher ) { countIncludeTeacher (); return ; } countOnlyStudents (); } public void countIncludeTeacher () {...} public void countOnlyStudents () {...} } In my unit test, I want to test the countPerson(boolean) function: public class SchoolTest { private School mSchool ;