mock

Mock static method with GroovyMock or similar in Spock

匿名 (未验证) 提交于 2019-12-03 01:06:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: First-timer here, apologies if I've missed anything. I'm hoping to get around a call to a static method using Spock. Feedback would be great With groovy mocks, I thought I'd be able to get past the static call but haven't found it. For background, I'm in the process of retrofitting tests in legacy java. Refactoring is prohibited. I'm using spock-0.7 with groovy-1.8. The call to the static method is chained with an instance call in this form: public class ClassUnderTest{ public void methodUnderTest(Parameter param){ //everything else

How to mock WCF client using Moq?

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: In my project I am using: SL5+ MVVM+ Prism + WCF + Rx + Moq + Silverlight Unit Testing Framework. I am new to unit-testing and have recently started into DI, Patterns (MVVM) etc. Hence the following code has a lot of scope for improvement (please fell free to reject the whole approach I am taking if you think so). To access my WCF services, I have created a factory class like below (again, it can be flawed, but please have a look): namespace SomeSolution { public class ServiceClientFactory : IServiceClientFactory { public

PowerMockito mock static method which throws exception

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have some static methods to mock using Mockito + PowerMock. Everything was correct until I tried to mock a static method which throws exception only (and do nothing else). My test class looks like this: top: @RunWith(PowerMockRunner.class) @PrepareForTest({Secure.class, User.class, StringUtils.class}) body: PowerMockito.mockStatic(Secure.class); Mockito.when(Secure.getCurrentUser()).thenReturn(user); PowerMockito.mockStatic(StringUtils.class); Mockito.when(StringUtils.isNullOrEmpty("whatever")).thenReturn(true); PowerMockito.mockStatic

Android mock gps provider

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My code snippet is: mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if(mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) { mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER); } mLocationManager.addTestProvider(LocationManager.GPS_PROVIDER, "requiresNetwork" == "", "requiresSatellite" == "", "requiresCell" == "", "hasMonetaryCost" == "", "supportsAltitude" == "", "supportsSpeed" == "", "supportsBearing" == "", android.location.Criteria.POWER_LOW, android.location.Criteria

Jest: How to globally mock node-uuid (or any other imported module)

匿名 (未验证) 提交于 2019-12-03 01:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Recently migrated from mocha to jest and I'm running into an issue. I have lots of warnings in my tests: [SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random() Now, adding the following line to each file fixes the issue, but only for that specific test file: jest.mock('node-uuid', () => ({ v4: jest.fn(() => 1) })); I'm hoping there's a way to mock node-uuid globally for all tests instead of individual files? I've done a bunch of searches and tried different techniques in my setup file, but to no avail. 回答1: You can

Adding Mock objects in Python

匿名 (未验证) 提交于 2019-12-03 01:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My code under test does sth like this: def to_be_tested(x): return round((x.a + x.b).c()) I would like to test it by passing a Mock object as x. I tried to do it like this: import unittest import unittest.mock class Test_X(unittest.TestCase): def test_x(self): m = unittest.mock.Mock() to_be_tested(m) # now check if the proper call has taken place on m The call to x.a and the one to x.b work as expected. They deliver new mock objects which can be asked how they were created (e. g. via q._mock_parent and q._mock_new_name ), so this step works

TestFixtureSetUpAttribute not found in JustMock?

匿名 (未验证) 提交于 2019-12-03 01:02:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm developing a TDD test with C#, .NET Framework 4.7, Nunit 3.8.0 and JustMock Lite 2017.2.821.1. When I do this: IGenericRepository<ProductionOrder> _proOrdRepository = Mock.Create<IGenericRepository<ProductionOrder>>(); I get the following exception: System.TypeInitializationException occurred HResult=0x80131534 Message=An exception occurred in the type initializer of 'Telerik.JustMock.Core.Context.MockingContext'. Source=Telerik.JustMock StackTrace: at Telerik.JustMock.Core.Context.MockingContext.get_CurrentRepository() at Telerik

Mock Retrofit service with Mockito causes ExceptionInInitializerError

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I tried with: androidTestCompile 'org.mockito:mockito-core:2.0.26-beta' ... public interface CustomerService { @POST("url") Observable<Session> createSession(@Body Credential credential); } ... @RunWith(AndroidJUnit4.class) @LargeTest public class LoginActivityTests { @Before public void setUp() { customerService = Mockito.mock(CustomerService.class); } @Test public void testLoginGoToNextScreen() throws Exception { doReturn(new Session"token")).when(customerService).createSession( new Credential("aa", "123", new Device("asd", ":)"))); } But

How to mock Google API AndroidPublisher request

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got a class that makes a request to the AndroidPublisher API to get the Purchases.Subscriptions.get resource. How does one mock out the OAuth request and call to get the subscription resource using the classes from the package com.google.api.client.testing? Here is the code to get the subscription: HttpTransport httpTransport = new NetHttpTransport.Builder().build(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); GoogleCredential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory

How to mock getJdbcTemplate().queryForObject()?

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I successfully mocked JdbcTemplate jdbcTemplate = getJdbcTemplate(); jdbcTemplate.queryForObject(); with JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class); when(jdbcTemplate.queryForObject(JdbcTwitterDao.SQL_SELECT_TWITTER, parameterizedRowMapper, 1)).thenReturn(expectedObject); Would you please let me know how to mock getJdbcTemplate().queryForObject(); I don't know which object I should mock. 回答1: If you are using JdbcDaoSupport as the base class to get the template, you can simply construct your DAO in the test and immediately