Why Did it Have to Be So Complicated Before??
2 min read
2 min read
I started web development using Java, basically during my studies at the university. When working on my bachelor degree thesis I needed a web server back-end system (to my mobile J2ME client); I decided to do it "right". Spring (especially Spring.Web) for the application server part and Hibernate for the object relational mapping. Although having quite a tough time getting up that initial learning curve, the result payed out so well: everything nicely decoupled and testable. I loved it. Then, when starting to work professionally as a .Net developer things got more painful...
After my studies (parallel to continuing with the MSc) I started to work professionally as a .Net developer, getting in touch with Asp.net (WebForms). And that just felt so strange. "Postback", "ViewState",...it was like imposing the WinForms technology on top of the web. Where was the usual request/response pattern?? Testability?? Sure, you can, but you don't want to fight with all the necessary setup you need to deal with when testing an ASP.net WebForms Page. The only way is to ensure that you keep that logic at a minimum possible and to rather defer it to some business layer class which is completely web-agnostic and which then can definitely be tested. And indeed, this is what our internal framework/class-library helped us to do.public class AccountController : Controllerand the corresponding (if also naive) test case:
{
...
[HttpPost]
public JsonResult Filter(Account account)
{
return Json(new List<Account>()
{
new Account()
});
}
...
}
[TestMethod]Now, isn't this simple??
public void TestFilter_FilterJuriStrumpflohner_ShouldReturnAccountInstance()
{
//Arrange
Account filterAccount = new Account()
{
Lastname = "Strumpflohner"
};
//Act
JsonResult result = new AccountController().Filter(filterAccount);
IList<Account> filterResult = result.Data as IList<Account>;
//Assert
Assert.AreEqual(1, filterResult.Count, "There should be only one result");
}