mock

Mocking HttpClient in unit tests

匿名 (未验证) 提交于 2019-12-03 01:55:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have some issues trying to wrap my code to be used in unit tests. The issues is this. I Have the interface IHttpHandler: public interface IHttpHandler { HttpClient client { get; } } And the class using it, HttpHandler: public class HttpHandler : IHttpHandler { public HttpClient client { get { return new HttpClient(); } } } And then the Connection class, which uses simpleIOC to inject the client implementation: public class Connection { private IHttpHandler _httpClient; public Connection(IHttpHandler httpClient) { _httpClient = httpClient;

Unable to run JUnit test with PowerMockRunner

匿名 (未验证) 提交于 2019-12-03 01:52:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a Gradle based Java project were I now want to mock a private method using PowerMock. The problem is that I am not able to use the PowerMockRunner as I always get the following exception when I add the @RunWith(org.powermock.modules.junit4.PowerMockRunner.class) annotation. Error: org . powermock . reflect . exceptions . FieldNotFoundException : Field 'fTestClass' was not found in class org . junit . internal . runners . MethodValidator . at org . powermock . reflect . internal . WhiteboxImpl . getInternalState (

Creating a mock in phpunit without mocking any methods?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: When I'm unit-testing my php code with PHPUnit, I'm trying to figure out the right way to mock an object without mocking any of its methods. The problem is that if I don't call getMockBuilder()->setMethods() , then all methods on the object will be mocked and I can't call the method I want to test; but if I do call setMethods() , then I need to tell it what method to mock but I don't want to mock any methods at all. But I need to create the mock so I can avoid calling the constructor in my test. Here's a trivial example of a method

java: how to mock Calendar.getInstance()?

匿名 (未验证) 提交于 2019-12-03 01:49:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In my code I have something like this: private void doSomething() { Calendar today = Calendar.getInstance(); .... } How can I "mock" it in my junit test to return a specific date? 回答1: As far as I see it you have three sensible options: Inject the Calendar instance in whatever method/class you set that day in. private void method(final Calendar cal) { Date today = cal.getTime(); } Use JodaTime instead of Calendar . This is less an option and more a case of a suggestion as JodaTime will make your life a lot easier. You will still need to

Mock an Eureka Feign Client for Unittesting

匿名 (未验证) 提交于 2019-12-03 01:48:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: i am using spring cloud's eureka and feign to communicate between some services (lets say A and B). Now id like to unittest my service layer of a single service (A). The problem is, that this service (A) is using a feign client to request some information of the other service (B). Running the unittests without any special configuration throws the following exception: java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-b => but i do not want any server to run. My

AndroidStudio/Gradle with powermock

匿名 (未验证) 提交于 2019-12-03 01:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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

What's the difference between a mock & stub?

匿名 (未验证) 提交于 2019-12-03 01:47:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've read various articles about mocking vs stubbing in testing, including Martin Fowler's Mocks Aren't Stubs , but still don't understand the difference. 回答1: Stub I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They would not do anything fancy and you would have already written the stubbed

How to use Google Mock with CppUnitTestFramework

匿名 (未验证) 提交于 2019-12-03 01:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: TL;DR: You can use GMock to add mocking capability to your Microsoft native c++ unit tests. See my answer below for details. I want to start adding mocks to my existing set of native unit tests. The tests are written using Microsoft's CppUnitTestFramework framework, which doesn't have support for mocking. I don't really want to convert the entire test suite to another framework just to add a few mocks. Google's GMock framework seems to provide everything I need and the documentation suggests it can be used with frameworks other

Mocking Logger and LoggerFactory with PowerMock and Mockito

匿名 (未验证) 提交于 2019-12-03 01:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following Logger I want to mock out, but to validate log entries are getting called, not for the content. private static Logger logger = LoggerFactory.getLogger(GoodbyeController.class); I want to Mock ANY class that is used for LoggerFactory.getLogger() but I could not find out how to do that. This is what I ended up with so far: @Before public void performBeforeEachTest() { PowerMockito.mockStatic(LoggerFactory.class); when(LoggerFactory.getLogger(GoodbyeController.class)). thenReturn(loggerMock); when(loggerMock.isDebugEnabled(

Cant mock static functions with powermock-easymock-testng (non-maven project)

匿名 (未验证) 提交于 2019-12-03 01:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: To tell you first, i have tried and tried it again and now i need some help Heres my code package staticPkg; public class Static { public static final String staticMethod() { System.out.println("Static method called"); return "Static called"; } } package staticPkg; public class TargetClass { Static staticClass; public String callHere() { return Static.staticMethod(); } } package staticPkg; import org.easymock.EasyMock; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org