mock

How to mock an import

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Module A includes import B at its top. However under test conditions I'd like to mock B in A (mock A.B ) and completely refrain from importing B . In fact, B isn't installed in the test environment on purpose. A is the unit under test. I have to import A with all its functionality. B is the module I need to mock. But how can I mock B within A and stop A from importing the real B, if the first thing A does is import B? (The reason B isn't installed is that I use pypy for quick testing and unfortunately B isn't compatible with pypy yet.) How

AndroidStudio/Gradle with powermock

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions. Could anybody show a correct way to do it? Thanks. 回答1: Im posting in order to help future readers, you need to add these dependencies for powermock in AS testCompile 'junit:junit:4.12' testCompile 'org.powermock:powermock-api-mockito:1.6.1' testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.1' testCompile 'org.powermock:powermock-module-junit4-rule:1.6.1' testCompile 'org.powermock:powermock

Mock non-virtual method C++ (gmock)

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have class class CSumWnd : public CBaseWnd { private: bool MethodA() } Please can you help how to mock MethodA() without making virtual, I didn't understand the concept of hi-perf dependency injection 回答1: It means you will have to templatize your production code. Using your example: CSumWind class definition: class CSumWnd : public CBaseWnd { private: bool MethodA() }; Mocked CSumWnd class definition: class MockCSumWnd : public CBaseWnd { private: MOCK_METHOD(MethodA, bool()); }; Production class which have to be tested with mocked class

Mock MVC - Add Request Parameter to test

匿名 (未验证) 提交于 2019-12-03 02:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using spring 3.2 mock mvc to test my controller.My code is @Autowired private Client client; @RequestMapping(value = "/user", method = RequestMethod.GET) public String initUserSearchForm(ModelMap modelMap) { User user = new User(); modelMap.addAttribute("User", user); return "user"; } @RequestMapping(value = "/byName", method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public @ResponseBody String getUserByName(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName, @ModelAttribute(

Unit testing with queries defined in extension methods

匿名 (未验证) 提交于 2019-12-03 02:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In my project I am using the following approach to querying data from the database: Use a generic repository that can return any type and is not bound to one type, i.e. IRepository.Get instead of IRepository .Get . NHibernates ISession is an example of such a repository. Use extension methods on IQueryable with a specific T to encapsulate recurring queries, e.g. public static IQueryable ByInvoiceType(this IQueryable q, InvoiceType invoiceType) { return q.Where(x => x.InvoiceType == invoiceType); } Usage would be like this: var result =

Android Unit Tests with Dagger 2

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an Android app that uses Dagger 2 for dependency injection. I am also using the latest gradle build tools that allow a build variant for unit testing and one for instrumentation tests. I am using java.util.Random in my app, and I want to mock this for testing. The classes I'm testing don't use any Android stuff, so they're just regular java classes. In my main code I define a Component in a class that extends the Application class, but in the unit tests I'm not using an Application . I tried defining a test Module and Component , but

Powermock does not create mock for java.time.ZonedDateTime

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I tried to create mock for java.time.ZonedDateTime using PowerMockito and I was expecting the mock object for ZonedDateTime. Instead, actual object is getting created and hence I cannot mock the methods of ZonedDateTime class. Following is my code snippet import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import static org.mockito.Matchers.any;

Mock java.time.format.DateTimeFormatter class

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to mock the DateTimeFormatter class. I've done the following: @RunWith(PowerMockRunner.class) @PrepareForTest({DateTimeFormatter.class}) public class UnitTest { private DateTimeFormatter mockDateFormatter; private AwesomeClass awesomeClass; @Before public void setUp() { mockDateFormatter = PowerMockito.mock(DateTimeFormatter.class); awesomeClass = new AwesomeClass(mockDateFormatter); } @Test public void shouldToTestSomethingAwesome() { // Other test code PowerMockito.when(mockDateFormatter.format(any(LocalDate.class))) .thenReturn

Using Mockito to test abstract classes

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'd like to test an abstract class. Sure, I can manually write a mock that inherits from the class. Can I do this using a mocking framework (I'm using Mockito) instead of hand-crafting my mock? How? 回答1: The following suggestion let's you test abstract classes without creating a "real" subclass - the Mock is the subclass. use Mockito.mock(My.class, Mockito.CALLS_REAL_METHODS) , then mock any abstract methods that are invoked. Example: public abstract class My { public Result methodUnderTest() { ... } protected abstract void

Spring JUnit: How to Mock autowired component in autowired component

匿名 (未验证) 提交于 2019-12-03 01:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've got a Spring component I'd like to test and this component has an autowired attribute which I need to change for the purpose of unit testing. The problem is, that the class uses the autowired component inside the post-construct method so I'm not able to replace it(i.e. via ReflectionTestUtils) before it's actually used. How should I do that? This is the class I want to test: @Component public final class TestedClass{ @Autowired private Resource resource; @PostConstruct private void init(){ //I need this to return different result