mock

How to mock riak java client?

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to unit test code that uses com.basho.riak:riak-client:2.0.0. I mocked all riak client classes and was hoping to get a useless but working test. However, this fails with a null pointer: java.lang.NullPointerException at com.basho.riak.client.api.commands.kv.KvResponseBase.convertValues(KvResponseBase.java:243) at com.basho.riak.client.api.commands.kv.KvResponseBase.getValue(KvResponseBase.java:150) at com.basho.riak.client.api.commands.kv.FetchValue$Response.getValue(FetchValue.java:171) My test looks like this: @Test public void

Mocking a base class method call with Moq

匿名 (未验证) 提交于 2019-12-03 01:00:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am modifiying a class method which formats some input paramater dates which are subsequently used as params in a method call into the base class (which lives in another assembly). I want to verify that the dates i pass in to my method are in the correct format when they are passed to the base class method so i would like to Moq the base class method call. Is this possible with Moq? 回答1: If I understand your question correctly, you have a class A defined in some other assembly, and then an class B implemented more or less like this: public

How to test method Jsoup.connect (static) using PowerMock?

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This is my code: public void analyze(String url) throws SiteBusinessException { Document doc = null; Response response = null; try { response = Jsoup.connect(url).execute(); doc = Jsoup.connect(url).get(); } catch (IOException e) { LOGGER.warn("Cannot analyze site [url={}, statusCode={}, statusMessage={} ]", new Object[] {url, response.statusCode(), response.statusMessage()}); throw new SiteBusinessException(response.statusMessage(), String.valueOf(response.statusCode())); } } How can I test this method using PowerMock? I want to write test

Original method still getting called in Moq even after CallBase = true/false

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Here's my code: public class Bar { } public class Foo { public string Name { get; set; } public Bar TheBar { get; set; } } public class Dependency { public Foo DoSomething(Expression<Func<Foo, bool>> exp1) { return new Foo(); } } public class Base { public Dependency Dependency { get; set; } public virtual Foo MethodA(Expression<Func<Foo, bool>> exp1, params Expression<Func<Foo, object>>[] exp2) { return Dependency.DoSomething(exp1); } } public class Derived : Base { public Foo DerviedMethod(string str) { return base.MethodA(e1 => e1.Name

Moq Equals Only Works With IEquatable

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using the Moq framework for unit tests, and I came across this interesting problem. public interface Bar : IEquatable<Bar> { } [TestClass] public class TestClass { Mock<Bar> a; Mock<Bar> b; public TestClass() { a = new Mock<Bar>(); b = new Mock<Bar>(); a.Setup(bar => bar.Equals(b.Object)).Returns(true); } [TestMethod] public void AssertEqualsTest() { Assert.AreEqual(a.Object, b.Object); //fails } [TestMethod] public void AssertIsTrueTest() { Assert.IsTrue(a.Object.Equals(b.Object)); //passes } } First Issue So Assert.AreEqual just fails.

How to mock the browser's timezone? [duplicate]

匿名 (未验证) 提交于 2019-12-03 00:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: This question already has an answer here: How to use a custom time in browser to test for client vs server time difference 8 answers I want to test a location feature in a web site, to make this test I need to try different time-zones. I obtain the timezone with a javascript code, calling the following function: var offset = new Date (). getTimezoneOffset (); Now this function returns to me 180 because I am in Argentina, I need to test with different time-zones. Somebody knows how to do this? Many thanks!! 回答1: The accepted answer

How can I easily mock out a static method in Java (jUnit4)

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do I easily mock out a static method in Java? I'm using Spring 2.5 and JUnit 4.4 @Service public class SomeServiceImpl implements SomeService { public Object doSomething() { Logger.getLogger(this.class); //a static method invoked. // ... } } I don't control the static method that my service needs to invoke so I cannot refactor it to be more unit-testable. I've used the Log4J Logger as an example, but the real static method is similar. It is not an option to change the static method. Doing Grails work, I'm used to using something like:

Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know there are already at least two same questions asked, but I still can't figure out why I am getting the exception. I need to unit test this method: void setEyelet(final PdfWriter printPdf, final float posX, final float posY) { InputStream is = WithDefinitions.class.getResourceAsStream(RES_EYELET); //RES_EYELET is a pdf. PdfContentByte canvas = printPdf.getDirectContent(); PdfReader reader = new PdfReader(is); PdfImportedPage page = printPdf.getImportedPage(reader, 1); canvas.addTemplate(page, posX, posY); reader.close(); } and verify

unable to put spy on HttpSession / Mockito

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want partial mocking on Httpsession but for that i need to spy it instead of mocking it , and it's a interface I can't get without request object which is already mocked. please help. In another word , how can I get a object of HttpSession without HttpServletRequest object. MORE DETAIL:: There is a servlet I want to test , servlet have session and puts "loginBean" (which contain loged in user related info) inside session, which I have mocked already and working fine , now IN GUI level , there are 2 tab , DetailSet1 , detailsSet2 , when you

Mocking ngrx/store

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This is in regards to the Angular 2 official release. I know that unit testing has changed drastically between beta, RC, and the official release. What's a good way to mock @ngrx/store in a unit test when it's used as a parameter in a constructor? It's not as simple as mocking a service. For example, if I wanted to mock a service, then I could do something like this: let serviceStub = { }; // not a true mocked service, just a stub, right? let de: DebugElement; let el: HTMLElement; let nativeEl: Element; let comp: Typeahead; let fixture: