I am trying to unit test the following method with Moq, but I am running into issues with the accessibility of manipulating some properties on these classes, and even mocking them up for that matter.
public string GetClientIpAddress(HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
return ((RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]).Address;
return "IP Address Unavailable";
}
In order to test, I have created an instance of HttpRequestMessage that I am passing in as a parameter. I am then adding a mock of HttpRequest and HttpContext, like so...
// Assign
var mockHttpRequestBase = new Mock<HttpRequest>();
mockHttpRequestBase.Setup(m => m.UserHostAddress).Returns("127.0.0.1");
var mockHttpContext = new Mock<HttpContext>();
mockHttpContext.Setup(m => m.Request).Returns(mockHttpRequestBase.Object);
var httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Properties.Add(new KeyValuePair<string, object>("MS_HttpContext", mockHttpRequestBase.Object));
var apiCachedController = new ApiCachedController();
// Act
var address = apiCachedController.GetClientIpAddress(httpRequestMessage);
// Assert
Assert.AreEqual(address, "127.0.0.1");
EDIT: Sorry for not being clearer on the specific problem(s) I'm having. HttpRequest is unable to be mocked. I get a NotSupportedException that "type to mock must be an interface or an abstract or non-sealed class." I've tried using HttpRequestBase and HttpContextWrapper in place of HttpRequest and HttpContext, respectively, but I receive an InvalidCastException stating, "Unable to cast object of type 'Castle.Proxies.HttpRequestBaseProxy' to type 'System.Web.HttpContextWrapper'."
Moq can't create a Mock object from a class itself, you've got to create (and use) an interface in your code and then create a Mock from that.
Related
This is the method to test:
It gets an URL and return a json after sending a GET request. It is a plain function which sits in a package rather than a method from a class. Same case for the extension method below.
fun getJson (url: String): String {
val connection = URL(url).openConnection() as HttpURLConnection
connection.requestMethod = "GET"
return connection.getResult()
}
This is the extension method:
It will start connecting and read from result stream.
internal fun HttpURLConnection.getResult(charset: Charset = Charsets.UTF_8): String {
this.connect()
return this.inputStream.bufferedReader(charset).use { it.readText() }
}
This is the test case:
I tried to mock the HttpURLConnection that is about to be used here and call the original method, then just call the method and assert whether the mock has been set with the expected value.
class Spike {
#Test
fun test_getJson() {
val expectedResult = "{ok: true}"
val mockConnection = mock(HttpURLConnection::class.java)
Mockito.`when`(mockConnection.getResult()).thenReturn(expectedResult)
getJson("http://www.google.com")
assertEquals("GET", mockConnection.requestMethod)
assertEquals("http://www.google.com", mockConnection.url.host)
}
}
This is the error
java.lang.IllegalStateException: this.inputStream must not be null at
my.spike.pack.http.UtilsKt.getResult(utils.kt:45)
It just like the mock is not working.
How to solve this without changing the signature of the getJson function?
This will not work because of the way Kotlin extension methods are implemented on the class / bytecode level.
What you see in source code is HttpURLConnection.getResult but on the class/bytecode level there is another file created with a static method: public final static getResult(HttpURLConnection, Charset).
Mockito cannot mock static methods. If you really have to mock one, then I think PowerMock is capable of doing that.
Edit:
If you have a module wide function then it is also generated on a class. Assuming you have a file StreamFunctions.kt with a function: doSomething then, there will be (by default) generated class StreamFunctionsKt with a static function doSomething. More details can be found here: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html
That should be as easy as
Mockito.`when`(mockConnection.inputStream).thenReturn(ByteArrayInputStream("test".toByteArray()))
I'm fairly new to using Moq and Nunit for unit testing and I'm having issues with one scenario. What I want is for my mock to have an out parameters which my system under test will then use to decide what action to take.
My system under test is an MVC API controller and in particular I'm trying to test the POST method. I want to return an error message for the object when validation fails.
Here is the method code for the controller:
public IHttpActionResult Post(Candidate candidate)
{
try
{
if(candidate==null)
return BadRequest();
IEnumerable<string> errors;
_candidateManager.InsertCandidate(candidate, out errors);
if (errors!=null && errors.Any())
return BadRequest(CreateErrorMessage("Invalid candidate: ", errors));
return CreatedAtRoute("DefaultApi", new {id = candidate.CandidateId}, candidate);
}
catch (Exception)
{
return InternalServerError();
}
}
This is my Unit Test Code:
[Test]
[Category("CandidateManagerController Unit Tests")]
public void Should_Return_Bad_Request_When_Creating_Invalid_Candidate()
{
IEnumerable<string> errors = new List<string>() {"error1", "error2"};
var mockManager = new Mock<ICandidateManager>();
mockManager.Setup(x => x.InsertCandidate(new Candidate(), out errors)).Callback(()=>GetErrors(errors));
var sut = new CandidateManagerController(mockManager.Object);
var actionResult = sut.Post(new Candidate());
Assert.IsInstanceOf<BadRequestResult>(actionResult);
}
What I expect is that when _candidateManager.InsertCandidate() is run then the errors variable is populated. However what is happening is that when you step through the controller code errors is null after _candidateManager.InsertCandidate() method is run.
If anyone has any ideas what I'm doing wrong or if what I want to do is not possible using Moq then please let me know.
Thanks
What you want to do is possible. If you look at the Quickstart docs at https://github.com/Moq/moq4/wiki/Quickstart, there is a section where it shows how you do setups for methods using out params. I have made two corrections to your code and it works.
You have to use the same candidate instance for both the mock setup and when you exercise the sut. Otherwise, Moq thinks that the two objects are different and your test setup becomes useless.
You don't have to use Callback in order to set the errors returned by the mocked CandidateManager.
Below is your test method with my changes.
[Test]
[Category("CandidateManagerController Unit Tests")]
public void Should_Return_Bad_Request_When_Creating_Invalid_Candidate()
{
IEnumerable<string> errors = new List<string>() {"error1", "error2"};
//instance to be used for both setup and test later
var candidate = new Candidate();
var mockManager = new Mock<ICandidateManager>();
//removed Callback
mockManager.Setup(x => x.InsertCandidate(candidate, out errors));
var sut = new CandidateManagerController(mockManager.Object);
var actionResult = sut.Post(candidate);
Assert.IsInstanceOf<BadRequestResult>(actionResult);
}
You have to make sure that when you call your SUT that you use the same instance passed to the out argument otherwise the call will fail.
In your example, the method under test passes a null instance into the mocked method thus negating the setup of the test.
If however you are not able to supply the same instances for the out then it doesn't look like you will be able to get a mock to pass successfully. Take a look a the Quick Start for Moq to get an understanding of it capabilities.
I want to create a unit test that validates a Logger.Write command is executed with the correct message in one of my MVC controllers.
I can mock the Listener that the Logger writes to, but I do not know how to reach the message that is stored. For example,
var mockListener = new Mock<MyTraceListener>();
// the .Write method is void so I can't use .Returns() on my mock
mockListener.Setup(listener => listener.Write(It.IsAny<string>()))
MyController controller = new MyController();
MyController.Index();
// and then the Index method calls the following Logger.Write() to the category that writes to MyTraceListener:
Logger.Write("test message", "MyCategory");
This write command to the logger does not return the input string or store it into a parameter for me to validate with an Assertion statement. Can I use one of the Moq verifies or setups or the .when (whatever this is) functions to get the message that is logged, or at least check that it executed?
Preferably, I want to store the log entry into a variable in my unit test so I can assert this:
Assert.AreEqual(loggedMessage, "test message");
If anyone knows of a strategy to do this I would greatly appreciate it.
Assuming you're using Entlib 5.0, there's already a mock point for you. Instead of using Logger.Write directly, instead inject an instance of LogWriter into your controller. You can then mock out the LogWriter object in your tests.
You want to use the Callback as a way to get the input from your Write method back to your unit test.
Because Loggers is static, you'll have to wrap the calls to those static methods in order to use Moq in your tests. The sample class below should be all you need to do to pull that off. You'll also have to update your code to use the wrapper, that might be painful.
Here is a sample test and class structure to achieve this.
public class Logger
{
public virtual void Write( string message, string category )
{
Loggers.Write( message, category );
}
}
[TestMethod]
public void SampleTest()
{
string input = string.Empty;
var mockLogger = new Mock<Logger>();
mockLogger.Setup( l => l.Write( It.IsAny<string>(), It.IsAny<string>() ) ).Callback( ( string message, string category ) => input = message );
mockLogger.Object.Write( "test", "category" );
Assert.AreEqual( "test", input );
}
Please note I'm using Moq version 4.0.10827.0.
I hope this helps!
and thank you in advance for any and all your assistance.
I have a method that I'm trying to test.
Within this method is a call to UserMembership.Validate()
//custom override but the code isn't functional yet and is outside the scope of the test.
I want to therefore mock (using moq) the return result so that the actual test of the method can succeed.
Here is the code
public LoginResponse Login(LoginRequest request)
{
var response = new LoginResponse(request.RequestId);
// Validate client tag and access token
if (!ValidateRequest(request, response, Validate.ClientTag | Validate.AccessToken))
return response;
if (!UserMembership.ValidateUser(request.UserName, request.Password))
{
response.Acknowledge = AcknowledgeType.Failure;
response.Messages = "Invalid username and/or password.";
//response.MessageCode = -4;
return response;
}
_userName = request.UserName;
return response;
}
So, my test is for LoginResponse() but I want to 'fake' the UserMembership return value (bool) to true...
Simple enough I'm sure for you guys.
TIA, Hugh.
You could probably re-title your question to "How do you use a mocking framework with unit testing 99% of the time," because you're right on track for doing just that - a very typical usage.
You're going to want to extract an interface from your UserMembership class (right click inside the class, select "refactor" and then "extract interface."), then use Moq to create mock instances of that interface for use within your tests. Then you can use Moq to "setup" the behavior of that mock to do anything you want it to during your test. The syntax would look like this:
var userMembershipMock = new Mock<IUserMembership>();
userMembershipMock.Setup(m=> m.ValidateUser(It.Is<string>(str=> str == "myUserName"), It.Is<string>(str=> str == "myPassword"))).Returns(true);
Then you would create a new instance of your class, passing in your mock instance of IUserMembership (but since you'll make your class's constructor takes an argument of the interface type, your class won't care whether you're passing it a mock or an actual UserMembership instance
MyClass myClass = new MyClass(userMembershipMock.Object);
after which you could begin actually testing the behavior of your MyClass:
var request = new LoginRequest { UserName = "myUserName", Password = "myPassword" };
LoginResponse response = myClass.Login(request);
And then you can assert that your class's response is what you expect:
Assert.AreEqual(AcknowledgeType.Success, response.Acknowledge);
or you can verify that your mock's method (or property) was invoked as you expected:
userMembershipMock.Verify(m=> m.ValidateUser(It.Is<string>(str=> str == "myUserName"), It.Is<string>(str=> str == "myPassword")), Times.Once());
and so on.
The Moq quick start page is kind of sort of a one-page read, and can teach you 99% of everything that you need to know to use it.
The only way I can think of to mock UserMembership in this case (assuming it's not a property) is to use an IoC framework like Castle Windsor or Ninject. When you use an IoC container you would refactor your calls to UserMembership into an interface (IUserMembership) and use the container to provide an implementation:
if (Container.Resolve<IUserMembership>().ValidateUser(request.UserName, request.Password))
Then in your unit test Setup you would register the implementation of IUserMembership to be the mock object:
var mock = new Mock<IUserMembership>();
Container.Register<IUserMemberhip>().Instance(mock.Object);
You would have to also create a production implementation. If this is the standard UserMembership class, this implementation will probably do nothing other than UserMembership. Although, there are other ways to mimic this kind of duck typing.
I have an annotated controller with a method that expects a model and a binding result
#RequestMapping(method = RequestMethod.POST)
public ModelAndView submit(#ModelAttribute(“user”) User user, BindingResult bindingResult) {
//do something
}
How do I test the binding result? If I call the method with a user and a binding result then I'm not testing the binding process. I figure there myst be something that takes a MockHttpServletRequest and returns the model and the binding result, any suggestions?
Are you trying to test the binding (which happens before this method would be called) or are you trying to test the "submit" handler method?
You can test the binding with something like this:
#Test
public void testHandlerMethod() {
final MockHttpServletRequest request = new MockHttpServletRequest("post", "/...");
request.setParameter("firstName", "Joe");
request.setParameter("lastName", "Smith");
final User user = new User();
final WebDataBinder binder = new WebDataBinder(user, "user");
binder.bind(new MutablePropertyValues(request.getParameterMap()));
final ModelAndView mv = controllerTestInstance.submit(user, binder.getBindingResult());
// Asserts...
}
You will probably find the spring-test-mvc project very relevant to what you're trying to achieve. The project is usable today and is quite simple overall but there will be some changes along the way, so if you need a stable API then this isn't for you just yet.