mocking

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

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...

"Short circuiting" void methods with Moq?

Hi, my team has made the decision recently to use Moq as our mocking framework for its tremendous flexibility and highly readable syntax. As we're new to it, I'm stumbling on what appears to be simple questions--searches (here, Google, etc.) find plenty of discussions on other nuances of Moq, but not necessarily what I'm after, and the ...

How can I measure the overhead of a mocking framework (TypeMock)?

I'm just beginning to evaluate mocking frameworks for my team and am wondering if anyone has any pointers to reference documentation or experience that you can share regarding the cost of mocking when doing performance tests. Links? Personal experience? Details appreciated. ...

Using TypeMock Isolator.Swap.AllInstances<T> in a Visual Studio Load Test?

I have the following tests setup. [TestClass, Isolated] public class TestClass { public TestClass() { } private TestContext testContextInstance; public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } [ClassInitialize, Isolated] ...

What resources are people using for TDD in ASP.NET MVC using Rhino Mocks?

Does any know or use and good resources for TTD with ASP.NET MVC specifically with Rhino Mocks. Do you prefer any other Mocking Framework? My choice on Rhino Mocks is simply because it seems the one which is most up to date and from what I have read is extremely capable! ...

Grails domain unit testing - mockFor()

Hi, This is the domain class: class Registration { String email String generatedKey def beforeInsert = { String newToken = GlobalHelper.getRandomString() generatedKey = newToken } } and this is the relevant part of the unit test: def c = mockFor(GlobalHelper) c.demand.static.getRandomString {-> return "...

Should I use mocking in production code?

I have a situation where I need to mock some code in production. This is for making one part of code, work in half functionality. I have to choose to write an empty classes (to implement the interface) , or use a mocking system like moq. So the question is, do the mocking systems hit the performance, or break some readability of th...

Moq: unit testing a method relying on HttpContext

Consider a method in a .NET assembly: public static string GetSecurityContextUserName() { //extract the username from request string sUser = HttpContext.Current.User.Identity.Name; //everything after the domain sUser = sUser.Substring(sUser.IndexOf("\\") + 1).ToLower(); return sUser; } I'd l...

Mocking out constructors in Grails

I'm new to testing in Grails, so I'm not sure if I'm taking the correct approach. I am trying to unit test a service (call it FooService) that has an instance of another class (call it Bar) as a property. Basically something like: class FooService { Bar bar void afterPropertiesSet() { bar = new Bar() } } So, I'm trying to test...

The right way to mock (with moq) methods that return mocked objects?

Which of these is correct? var mockLogger = new Mock<EntLibLogger>(); mockLogger.Setup(i => i.CreateTracer(It.IsAny<string>())) .Returns((string operationName) => { var mockTracer = new Mock<EntLibTracer>(operationName); mockTracer.Setup(i => i.IsTracingEnabled()) .Returns(true); ...

Usage of Assert.Inconclusive

Hi, Im wondering how someone should use Assert.Inconclusive(). I'm using it if my Unit test would be about to fail for a reason other than what it is for. E.g. i have a method on a class that calculates the sum of an array of ints. On the same class there is also a method to calculate the average of the element. It is implemented by ca...

Can this be mocked with Moq?

I am working on mocking some external dependencies and am having trouble with one 3rd party class that takes in it's constructor an instance of another 3rd party class. Hopefully the SO community can give me some direction. I want to create a mock instance of SomeRelatedLibraryClass that takes in it's constructor a mock instance of Som...

Mocking inside a Java class

So I have this GWT code that handles RPC requests maintain states(ready, waiting, error etc). And I would like to check if the class change its states correctly after each call, set response variables etc. Now how should I proceed to test that without making actual requests to the server(that could run into errors in the server it self)...

How could I refactor this factory-type method and database call to be testable?

I'm trying to learn how to do Unit Testing and Mocking. I understand some of the principles of TDD and basic testing. However, I'm looking at refactoring the below code that was written without tests and am trying to understand how it needs to change in order to make it testable. public class AgentRepository { public Agent Select(in...

Mocking functions with custom argument matchers in JSMock

I was reviewing JSMock, and admittedly the most complete documentation I've found are the examples at http://jsmock.sourceforge.net/examples/. I have some questions about argument matching. The example at http://jsmock.sourceforge.net/examples/ demonstrates argument matching using the TypeOf.isA(). I wonder, are there any other matc...

Testing authenticated file uploads in merb

This is something that has been driving me mad over the past few days. I have an action which allows authenticated users to upload assets to the site. I know that the controller action is correct as I can run through the process manually however I want to test it using rspec. I have to use the request helper so I can reuse an authentica...

Mocking PHP functions in unit tests

I'm unit-testing some PHP code with SimpleTest and I've run into trouble. In my tests of a database class I want to be able to set an expectation for PHPs mysql functions. In my tests of a wrapper class for the mail function I want to mock PHPs mail function. These are just some examples. The point is: I don't (always) want to test if m...

Ninject equivalent of Unity RegisterInstance method

Does Ninject have and equivalent method for unity's registerinstance. I want to create a mock object and register it. Thanks ...

Has anyone successfully mocked the Socket class in .NET?

I'm trying to mock out the System.net.Sockets.Socket class in C# - I tried using NUnit mocks but it can't mock concrete classes. I also tried using Rhino Mocks but it seemed to use a real version of the class because it threw a SocketException when Send(byte[]) was called. Has anyone successfully created and used a Socket mock using an...

Creating mock Objects in PHP unit

Hi, I've searched but can't quite find what I'm looking for and the manual isn't much help in this respect. I'm fairly new to unit testing, so not sure if I'm on the right track at all. Anyway, onto the question. I have a class: <?php class testClass { public function doSomething($array_of_stuff) { return Anothe...