unit test convert to dto extension method with fluentconversion - unit-testing

if i have a extension method that converts an Person object to a PersonDTO then
should i unit test that the conversion occurs correctly
if so and i use fluentassertions how do i assert that the conversion is correct
my extension method is:
public static PersonDTO ToDto (this Person person)
{
if (person == null) return null;
return new PersonDTO
{
FirstName = person.FirstName,
LastName = person.LastName,
Dob = person.Dob,
Identifier= person.Id
};
}
my person object has other properties that are not being mapped.
to get the personDTO object i would therefore do something similar to below:
var newPerson = new Person{ //set properties here };
var personDto = newPerson.ToDto();

First, yes, this code should be tested.
In order to check that the conversion is correct, you have to build the expected result manually, and then assert that the result of the function is the same:
var person = new Person{ ... };
var expectedPersonDto = new PersonDto{ ... };
person.ToDto().Should().BeEquivalentTo(expectedPersonDto);
See this for more information on comparing object graphs.

Related

#RequestParam spring MVC , make one of two request params mandatory

I am writing a service wherein I take in either an id or a location and I want to enforce the constraint that either the id or the location must be specified in my #Controller
#Controller
public class HelloController {
#RequestMapping(value="/loc.json",method = RequestMethod.GET)
public #ResponseBody String localiaztionRequest(#RequestParam(value = "location", required = false) String callback
,#RequestParam(value = "id", required = false) String uuid
,#RequestParam(value = "callback", required = false) String callback) {
//model.addAttribute("message", "Hello world!");
return "hello";
}
For clarity, I want each request to send either the location parameter or the id parameter. How do I enforce such a constraint on a pair of input parameters? Also as an aside could someone please explain to me the use of ModelMap , what is the effect of model.addAttribute("message","Hello World!") ?
Sorry if the questions seem rather naive, I'm extremely new to the spring framework.
Thanks in advance.
I think you should split it into two different controller methods
#RequestMapping(value="/loc.json",method = RequestMethod.GET, params={"location"})
public #ResponseBody String localizationRequestByLoc(#RequestParam String location, #RequestParam String callback) {
//model.addAttribute("message", "Hello world!");
return "hello";
}
#RequestMapping(value="/loc.json",method = RequestMethod.GET, params={"id"})
public #ResponseBody String localizationRequestById(#RequestParam String id, #RequestParam String callback) {
//model.addAttribute("message", "Hello world!");
return "hello";
}
One way to accomplish this (although indirect) would be to create a new object model representing the data to be passed in and another representing the data type and requiring both elements.
public enum MethodType
{
First,
Second
}
public class Filter
{
[Required]
public MethodType {get;set;}
[Required]
public string Data {get;set;}
}
use this object for the action method and view
and let the action method in the controller convert the object

Testing antlr4 visitor rules

I've got a fairly complicated antlr4 grammar that utilizes a visitor pattern. I'd like to test parts of the visitor. What's a good way to test individual visit rules?
My visitor has tons of rules like this that I want to test:
#Override
public Object visitQux(ExclParser.QuxContext ctx) {
return visitChildren(ctx);
}
And my test code basically is the following:
PrintStream ps = new PrintStream(stdError, false /* autoFlush */, "UTF-8")
ANTLRInputStream input = new ANTLRInputStream(is);
MyLexer lexer = new MyLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokens);
parser.removeErrorListeners();
MyErrorListener errorListener = new MyErrorListener(ps, filename);
parser.addErrorListener(errorListener);
MyVisitor visitor = new MyVisitor();
visitor.setParser(filename, parser, errorListener);
ParseTree tree = parser.qux(); // <--- This is the line I want to vary.
Object result = visitor.visit(tree);
assertThat(describeExpectation(), result, equalTo(this.expectedOutput));
Ideally I would be able to test any visitor using a parameterized test. But to get the parse tree I want to visit (parser.qux) I can't specify any variant of qux() in a table because parser.qux() is not static.
Any thoughts?
I created an example project on how ANTLR-visitors could be tested (using Mockito&TestNG). The full source code can be found on GitHub. Here are the most important parts:
public class MyVisitor extends DemoBaseVisitor<String> {
#Override
public String visitPlus(final DemoParser.PlusContext ctx) {
return visit(ctx.left) + " PLUS " + visit(ctx.right);
}
#Override
public String visitLiteralNumber(final DemoParser.LiteralNumberContext ctx) {
return ctx.getText();
}
}
Any my test for that visitor:
public class MyVisitorTest {
private final MyVisitor myVisitor = new MyVisitor();
#Test
public void visitPlus_joinsOperatorsWithWordPLUSAsSeparator() throws Exception {
// setup
final DemoParser.PlusContext plusNode = mock(DemoParser.PlusContext.class);
plusNode.left = mockForVisitorResult(DemoParser.ExpressionContext.class, "2");
plusNode.right = mockForVisitorResult(DemoParser.ExpressionContext.class, "4");
// execution
final String actual = myVisitor.visitPlus(plusNode);
// evaluation
assertEquals(actual, "2 PLUS 4");
}
private<T extends RuleContext> T mockForVisitorResult(final Class<T> nodeType, final String visitResult) {
final T mock = mock(nodeType);
when(mock.accept(myVisitor)).thenReturn(visitResult);
return mock;
}
#Test
public void visitLiteralNumber_returnsTextValueOfNumber() throws Exception {
// setup
final DemoParser.LiteralNumberContext literalNumberNode = mock(DemoParser.LiteralNumberContext.class);
when(literalNumberNode.getText()).thenReturn("42");
// execution
final String actual = myVisitor.visitLiteralNumber(literalNumberNode);
// evaluation
assertEquals(actual, "42");
}
}
In your special example where you have a method which calls visitChildren() your would test that calling that method returns the aggregated result of visiting all child nodes (what the aggregation is depends on your implementation of the aggregateResult method).
Reflection may be the right answer here:
Method method = MyParser.class.getDeclaredMethod("qux");
ParseTree tree = (ParseTree) method.invoke(parser);
is a suitable replacement for:
ParseTree tree = parser.qux();

Issues with mocking methods using Moq

I have the following class that has two static methods Retrieve and RetrieveWithQuery.
Below the classes listed here, I have included a snippet of the test.
All but the last assert of the test fails with the following message:
Failed TestMethod2 MoqTest Assert.AreEqual failed. Expected:. Actual:<(null)>.
I understand that the problem may be that the query that I setup in the mock
is a different instance from the query used in the RetrieveWithQuery method.
And that is why is would be returning null.
In a perfect world I would simply re-factor the service class, unfortunately I am
working with legacy code that is already production. The goal is to first complete
tests, then re-factor code and run regression testing before updating production
environment.
Is there a workaround or different way to test this?
public class MyService
{
public virtual string RetrieveMethod(string account)
{
if (account == "The abc company")
{
return "Peter Smith";
}
return "John Doe";
}
public virtual string RetrieveMethod(MyQuery query)
{
return RetrieveMethod(query.QueryString);
}
public static string Retrieve(MyService service, string value)
{
return service.RetrieveMethod(value);
}
public static string RetrieveWithQuery(MyService service, string value)
{
var query = new MyQuery
{
QueryString = value
};
return service.RetrieveMethod(query);
}
}
public class MyQuery
{
public string QueryString;
}
[TestMethod]
public void TestMethod2()
{
var mockService = new Mock<MyService>();
const string company = "The abc company";
const string expectedContact = "Peter Smith";
var queryAccount = new MyQuery
{
QueryString = company
};
// Setup base retrieve
mockService.Setup(myServ => myServ.RetrieveMethod(company)).Returns(expectedContact);
// Setup base retrieve with query
mockService.Setup(myServ => myServ.RetrieveMethod(queryAccount)).Returns(expectedContact);
// test base retrieve with query - PASS
Assert.AreEqual(expectedContact, mockService.Object.RetrieveMethod(queryAccount));
// test static method retrieve - PASS
Assert.AreEqual(expectedContact, MyService.Retrieve(mockService.Object, company));
// test static method retrieve with query - FAIL
Assert.AreEqual(expectedContact, MyService.RetrieveWithQuery(mockService.Object, company));
}
Try this for your setup:
// Setup base retrieve with query
mockService.Setup(myServ => myServ.RetrieveMethod(It.Is<Query>(q=>q.QueryString == queryAccount.QueryString)).Returns(expectedContact);
Or you could overload on Equals for Query so that the Query that gets created is equal to expectedQuery.
The Moq QuickStart page has good examples of this and more which should help a lot.

UNIT test dto and domain objects

I am planning to write an abstract class kind of thing for testing all my DTOs and DOMAIN objects. This class will take in templatable object (generic type)and use reflection to get the properties' types within and will assign some default values to the primitive types identified and later will assert these type values by accessing them. This way whenever my DTO tests inherit this class, most of the code is tested with one line of code written in the test. This is just an idea and want to know from you all if I am reinventing the wheel if something like this already exists? If there is a better way to test DTO's and domain object with less and resuable code.
I don't think that this is a good approach for testing Domain objects. By definition, these objects encapsulate data and related behavior, they suppose to be much more than just dumb data containers with getters and setters. You will have to hand write unit tests for these objects just like you hand-wrote the objects themselves. This is where you actually suppose to be spending time according to DDD.
Regarding DTOs you may want to look at this question.
My advice :
Don't unit test DTO's. These are just simple data structures with a bunch of getters and setters and no behavior. Getters and setters are too dumb to be tested (unless they encapsulate some kind of conditional logic which is rarely the case with DTO's).
Don't try to automate or genericize your domain object tests. I can't see how code that tests their behavior can be reused anyway since they all have a different behavior by definition.
Even though I think is kind of worthless to unit test DTOs, based on #Dmitry's answer I came up with this class:
[TestClass]
public class PeopleTest
{
[TestMethod]
public void OneObjectNull()
{
Person obj1 = null;
var obj2 = new Person
{
Id = "101",
Name = "George Waits",
Address = "Lake Palmer 10"
};
Assert.AreNotEqual(obj1, obj2);
Assert.AreNotEqual(obj2, obj1);
}
[TestMethod]
public void DeepEqual()
{
var obj1 = new Person
{
Id = "101",
Name = "George Waits",
Address = "Lake Palmer 10"
};
var peolpleList1 = new List<Person> { obj1 };
var peolpleList2 = new List<Person> { obj1 };
Assert.AreEqual(obj1, obj1);
CollectionAssert.AreEqual(peolpleList1, peolpleList2);
}
[TestMethod]
public void DeepNotEqual()
{
var obj1 = new Person
{
Id = "101",
Name = "George Waits",
Address = "Lake Palmer 10"
};
var obj2 = new Person
{
Id = "102",
Name = "Rachel Smith",
Address = "Lake Palmer 10"
};
var peolpleList1 = new List<Person> { obj1 };
var peolpleList2 = new List<Person> { obj2 };
Assert.AreNotEqual(peolpleList1, peolpleList2);
var group1 = new KeyValuePair<string, List<Person>>("group1", peolpleList1);
var group2 = new KeyValuePair<string, List<Person>>("group2", peolpleList2);
Assert.AreNotEqual(group1, group2);
}
[TestMethod]
public void PropertyPositive()
{
var obj1 = new Person
{
Id = "101",
Name = "George Waits",
Address = "Lake Palmer 10"
};
obj1.Address = "Parker av 101";
var obj2 = new Person
{
Id = "102",
Name = "Rachel Smith",
Address = "Lake Palmer 10"
};
obj1.Address = "Listener av 45";
Assert.AreNotEqual(obj1, obj2);
}
}

Rhino Mocks - Mock a method call to Service inside a property

I am trying to test that the property gets it's value from the return of a Service call, but I am having trouble mocking the service call.
Here is my property:
public ICountry Country
{
get
{
if (_country == null)
{
ICountryService countryService = new CountryService();
_country = countryService.GetCountryForCountryId(_address.CountryId);
}
return _country;
}
}
Here is my attempt at testing this:
[TestMethod]
public void Country_should_return_Country_from_CountryService()
{
const string countryId = "US";
_address.CountryId = countryId;
var address = MockRepository.GenerateStub<Address>(_address);
var country = MockRepository.GenerateMock<ICountry>();
var countryService = MockRepository.GenerateStub<ICountryService>();
countryService.Stub(x => x.GetCountryForCountryId(countryId)).IgnoreArguments().Return(country);
Assert.AreEqual(address.Country, country);
}
I keep getting an error because the real countryService is being called, not my mock. I am using MsTest and Rhino Mocks. What am I doing wrong?
You problem is that the property is directly constructing the dependency. Due to this the mock service is not getting called, the actual real CountryService implementation is getting called.
A way around this might be to utilize constructor injection of a CountryService factory (or the service itself) in the other objects (Address?) constructor. This way you could get your fake CountryService (the mock) to be returned and be the one called by the method
For example:
private ICountryService _countryService;
//constructor
public OuterObject(ICountryService countryService)
{
//maybe guard clause
_countryService = countryService;
}
public ICountry Country
{
get
{
if (_country == null)
{
_country = _countryService.GetCountryForCountryId(_address.CountryId);
}
return _country;
}
}
You would need to then pass the mocked ICountryService into the other objects constructor in your unit test