How do I unit test routes for web forms? - unit-testing

I have a web form such as mysite.com/list.aspx?state=florida&city=miami that I want users to browse using mysite.com/florida/miami/ and I'm using routing to do so. Then instead of using query string parameters, I end up having to use HttpContext.Current.Items[key] to retrieve the values on my list.aspx page. I have included the code below.
I would like to know what the best practices are to unit test this. Also, is there a better way to implement this without changing my code on the list.aspx page?
Code:
Sample of my Global.asax file:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("{state}", new CustomRouteHandler("/list.aspx")));
routes.Add(new Route("{state}/{city}", new CustomRouteHandler("/list.aspx")));
}
Sample of the CustomerRouteHandler:
public class CustomRouteHandler : IRouteHandler
{
public CustomRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public string VirtualPath { get; private set; }
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
foreach (var urlParm in requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;
}
IHttpHandler page = BuildManager.CreateInstanceFromVirtualPath (VirtualPath, typeof(Page)) as IHttpHandler;
return page;
}
}

You could use HttpUnit to test that all the url endpoints work correctly.

Related

How can I use MockWebServer for Junit test cases of webclient?

I have a spring-boot application which calls some third party URL (let's say http://example.com/someUri) using webclient(I have used application-dev.properties for injecting this url in my application to achieve loose coupling) and consumes the response and use it in my application.
It's my first time when I am going to write test cases for webclient. and there I used #SprintBootTest.
I found that there are two ways where I can test my webclient with third party Api call by mocking the api call and make it call to my local url(which will be using url(http://localhost:{portNumber}/someUri) from my testing properties file: src/test/resources/application.properties) where It will be giving some mockedResponse in return to my real client:
Using wiremock
Using MockWebServer
consider above code for better understanding:
#Service
Class SampleService{
#Value("${sample.url}")
private String sampleUrl;
public String dummyClient() {
String sample =webClient.get()
.uri(sampleUrl)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToMono(String.class)
.block();
return sample;
}
}
application-dev.properties:
sample.url:http://example.com/someUri
src/test/resouces/application.properties:
http://localhost:8090/someUri
Testing class:
#SpringBootTest
public class sampleTestingClass {
#Autowired
private SampleService sampleService;
#Value("${sample.url}")
private String sampleUrl;
public static MockWebServer mockWebServer = new MockWebServer();
#BeforeAll
static void setUp() throws IOException {
mockWebServer.start(8090);
}
#AfterAll
static void tearUp() throws IOException {
mockWebServer.close();
}
HttpUrl url = mockWebServer.url("/someUri");
mockWebServer
.enqueue(
new MockResponse()
.setResponseCode(200)
.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.setBody("Sample Successful"));
String sample = sampleService.dummyClient();
assertEquals(sample ,matches("Sample Successful"));
}
}
but this code isn't working. it's giving me above error:
java.lang.NullPointerException
It will be really helpful if anybody knows how this can be fixed to achieve my unit testing using mocked Url? Thanks in advance!
Here is a working example:
#Component
public class QuotesClient {
private final WebClient webClient;
public QuotesClient(WebClient.Builder builder, #Value("${client.baseUrl}") String baseUrl) {
this.webClient = builder.baseUrl(baseUrl).build();
}
public JsonNode getData() {
return this.webClient
.get()
.retrieve()
.bodyToMono(JsonNode.class)
.block();
}
}
Using the WebClient.Builder is optional.
The corresponding test can look like the following:
class QuotesClientTest {
private QuotesClient quotesClient;
private MockWebServer server;
#BeforeEach
public void setup() {
this.server = new MockWebServer();
this.quotesClient = new QuotesClient(WebClient.builder(), server.url("/").toString());
}
#Test
public void test() {
server.enqueue(new MockResponse()
.setStatus("HTTP/1.1 200")
.setBody("{\"bar\":\"barbar\",\"foo\":\"foofoo\"}")
.addHeader("Content-Type", "application/json"));
JsonNode data = quotesClient.getData();
assertNotNull(data);
System.out.println(data);
}
}
If you are searching for a similar setup using WireMock, Spring Boot, and JUnit 5, take a look at the linked guide.

How to get db data back via repository service with MVC and Moq

I need to retrieve data from the db, via a service, in order to test captured data for structural validity.
i.e. The db data specifies the sizes of parts of a captured concatenated string, (in a certain order), which I mock. So no worries there.
But I don't want to mock the service's data.
I want to actually retrieve the db data, which can change/vary vastly for different clients, and different scenarios, or be changed regularly.
So, changing the mocked data for each scenario is not feasible.
The Service implements an interface and also injects the Data Access Layer's interface.
I do set 'CallBase' as true, but I'm not getting db Data back.
Please help.
Thanks
public interface I_PartDao
{
ExBool List(out List<GXL_PartSizes> _PartSizes);
}
public class GXL_PartSizes
{
public int? ID { get; set; }
public int? PartLength { get; set; }
public int? SortOrder { get; set; }
public GXL_PartSizes()
{
this.ID = null;
this.PartLength = null;
this.SortOrder = null;
}
}
// Service Layer
public interface I_PartBo
{
ExBool List(out List<GXL_PartSizes> _PartSizes);
}
public class PartBo : I_PartBo
{
// For the injection of the Dao services (Dao handles the Ado CRUD operations against MSSQL dbase)
private I_PartDao PartDao;
public PartBo(I_PartDao dao_Part)
{
this.PartDao = dao_Part ?? throw new ArgumentNullException("dao_Part");
}
public ExBool List(out List<GXL_PartSizes> _PartSizes)
{
return this.PartDao.List(out _PartSizes);
}
}
[TestClass]
public class GXL_ConcatenatedStructures_Test
{
ExBool result = new ExBool(); // A class to handle error messages and statuses
private List<GXL_PartSizes> partSizes;
[TestMethod]
public void Test_Part_1_Length_matches_dbStructure_for_part1()
{
// Arrange
var mockService = new Mock<I_PartBo>();
// This is retrieved from the data posted back to the controller,..but for here and now, just a local populated strign var
var concatenatedString_part1 = "ABC";
// Act
//mockService.CallBase = true;
mockService
.Setup(x => x.List(out partSizes))
.Returns(result);
// Assert
Assert.AreEqual(concatenatedString_part1, partSizes[0].PartLength);
}
}
I thought that there was a way to do what I wanted to, by using moq.
However,
by just using the normal concrete classes, as per the usual (sans DI) way, The db data is retrieved.
i.e.
NamespaceX.GLX_SetupDao dao = new NamespaceX.GLXSetupDao("data source=LAPTOP-a; Initial
Catalog=aaaa; Integrated Security=True;");
GLX_SetupBo boService = new GLX_SetupBo(dao);
result = boService.List(out List<GXL_PartSizes> sizes);

How to send a property as a string in RestEasy response?

I have a class called Product which has a property called id of type long. Below is the class
public class Product {
private long id;
}
The value of id is beyond the value which javascript can handle. I realized this after seeing the below link
Parse json in javascript - long numbers get rounded
I dont want to declare the field as String in the domain class. But I want to say to RestEasy that it has to send the value as a string in the json response.
How can I do this? I dont want to use any third party api. Is it possible in RestEasy. I have gone through the documentation but did not find any such annotation or may be I did not go through the documentation properly.
Can anyone please help. Thanks all in advance.
If you are using Jackson as JSON Serializer you can extend the JacksonJsonProvider:
#Provider
public class JsonProvider extends org.codehaus.jackson.JacksonJsonProvider {
public JsonProvider() {
ObjectMapper objectMapper = locateMapper(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
objectMapper.configure(org.codehaus.jackson.JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
}
}
If you are using Jettison you can register a custom XmlAdapter:
public class LongAdapter extends XmlAdapter<String, Long> {
#Override
public String marshal(Long id) throws Exception {
if (id == null) {
return "";
}
return id.toString();
}
#Override
public Long unmarshal(String id) throws Exception {
return Long.parseLong(id);
}
}

How do I run my NUnit test cases using Selenium to run against different environments?

I have written NUnit test cases using Selenium to test a web application. And I would like to run the same test cases against different environments (e.g. QA, Staging, & Production) What's the easiest way to achieve that?
NUnit supports parametrised test fixtures as well as parametrised tests. So the first thing is that are you going to want to run specific tests against different environments, or is it the entire test fixture will be rerun for both environments?
I ask because the answer to this determines where you will pass the parameter (the environment). If you are just wanting to rerun the whole test fixture, you should pass the environment in at a test fixture level, that is to create parametrised test fixtures. If you want to run only particular tests against those environment, you'll have to pass it in to each individual test case. An example is below of how I've gone about the same sort of thing:
First create a way to define what 'environment' the tests can 'attach' to. I'd suggest perhaps shoving this into the app.config and have a 'Settings' class and an enum to go with it:
public enum Environment
{
QA,
Production,
Hotfix,
Development
}
public class Settings
{
public static string QAUrl { get { return "some url"; } }
public static string ProductionUrl { get { return "some url"; } }
public static string HotfixUrl { get { return "some url"; } }
public static string DevUrl { get { return "some url"; } }
}
The above "some url" would be read from your configuration file or hardcoded, however you please.
We've now got a concept of an environment, and it's URL, but they are not linked together or related in any way. You would ideally want to give it the 'QA' value of your enum and then it will sort out the URL for you.
Next create a base test fixture that all your test fixtures can inherit from, which keeps hold of the current environment. We also create a Dictionary that now relates the environment value to it's URL:
public class BaseTestFixture
{
private Dictionary<Environment, string> PossibleEnvironments
{
get
{
return new Dictionary<Environment, string>()
{
{ Environment.QA, Settings.QAUrl },
{ Environment.Production, Settings.ProductionUrl },
{ Environment.Hotfix, Settings.HotfixUrl },
{ Environment.Development, Settings.DevelopmentUrl },
}
}
}
private Environment CurrentEnvironment { get; set; }
public BaseTestFixture(Environment environment)
{
CurrentEnvironment = environment;
}
}
You could probably use Reflection to have it work out what URL's map to what enum value's.
So cool, we've got an environment we can run against. A sample test to go to login as an administrator to your site:
public class LoginToSite
{
[Test]
public void CanAdministratorSeeAdministratorMenu()
{
// go to the site
driver.Navigate().GoToUrl("production site");
// login as administrator
}
}
How do we get this to go to the specific URL? Let's modify our base class a little...
public class BaseTestFixture
{
private Dictionary<Environment, string> PossibleEnvironments
{
get
{
return new Dictionary<Environment, string>()
{
{ Environment.QA, Settings.QAUrl },
{ Environment.Production, Settings.ProductionUrl },
{ Environment.Hotfix, Settings.HotfixUrl },
{ Environment.Development, Settings.DevelopmentUrl },
}
}
}
private Environment CurrentEnvironment { get; set; }
protected string CurrentEnvironmentURL
{
get
{
string url;
if (PossibleEnviroments.TryGetValue(CurrentEnviroment, out url))
{
return url;
}
throw new InvalidOperationException(string.Format("The current environment ({0}) is not valid or does not have a mapped URL!", CurrentEnviroment));
}
}
public BaseTestFixture(Environment environment)
{
CurrentEnvironment = environment;
}
public BaseTestFixture()
{
}
}
Our base class now can tell us, depending on what environment we are in, what page to go to...
So we now have this test, inheriting from our base:
public class LoginToSite : BaseTestFixture
{
[Test]
public void CanAdministratorSeeAdministratorMenu()
{
// go to the site
driver.Navigate().GoToUrl(CurrentEnvironmentURL);
// login as administrator
}
}
However, that's great, but the above won't compile...why? We are not actually giving it an environment yet so we must pass one in...
[TestFixture(Environment.QA)]
public class LoginToSite : BaseTestFixture
{
[Test]
public void CanAdministratorSeeAdministratorMenu()
{
// go to the site
driver.Navigate().GoToUrl(CurrentEnvironmentURL);
// login as administrator
}
}
That's great, it now has the environment passed in, the checking of the URL etc are all done in the background for you now...however this still won't compile. Since we are using inheritance, we have to have a constructor to pass it down for us:
public LoginToSite(Environment currentEnvironment)
{
CurrentEnvironment = currentEnvironment;
}
Et voilĂ .
As for specific test cases, this is a little easier, taking our test case from earlier:
public class LoginToSite
{
[TestCase(Environment.QA)]
public void CanAdministratorSeeAdministratorMenu(Environment environment)
{
// go to the site
driver.Navigate().GoToUrl("production site");
// login as administrator
}
}
Which would pass in the environment into that specific test case. You would then need a new Settings class of some sort, to do the environment checking for you (in a similar way as I did before):
public class EnvironmentHelper
{
private static Dictionary<Environment, string> PossibleEnvironments
{
get
{
return new Dictionary<Environment, string>()
{
{ Environment.QA, Settings.QAUrl },
{ Environment.Production, Settings.ProductionUrl },
{ Environment.Hotfix, Settings.HotfixUrl },
{ Environment.Development, Settings.DevelopmentUrl },
}
}
}
public static string GetURL(Environment environment)
{
string url;
if (PossibleEnviroments.TryGetValue(environment, out url))
{
return url;
}
throw new InvalidOperationException(string.Format("The current environment ({0}) is not valid or does not have a mapped URL!", environment));
}
}
Best way would be to use variables instead of hard coded links for all functions. So that it can be changed when needed to change the environment. An easier approach would be to read links from a notepad/excel file .

Why is AutoFixture Customization causing inherited properties to not be filled?

I wrote the following customization and have it applied as part of a composite on most of my tests. My entities have a read-only Id, but I'm using their SetId method in this customization to make sure all entities have some Id if they are transient (don't have an Id already).
public class SetEntityIdCustomization : ICustomization {
public void Customize(IFixture fixture) {
var engine = ((Fixture)fixture).Engine;
fixture.Customizations.Add(new Postprocessor(
engine, o => {
var entity = o as BaseEntity;
if (entity == null || !entity.IsTransient()) {
return;
}
entity.SetId(fixture.CreateAnonymous<Guid>());
}));
}
}
This has been working great, until I discovered a very odd thing today. If I feed a test one of my entities that directly inherits from BaseEntity, all is well and it's writeable properties are auto-filled. However, if I ask for an entity that inherits from something further down from BaseEntity, my customization prevents the properties from auto-filling.
The User entity in this test method is filled properly:
public class User : BaseEntity {
public string Email { get; set; }
public int CoolThings { get; set; }
}
...
[Theory, AutoDomainData]
public void SomeTest(User user, ...) {
// user.Email and user.CoolThings have auto-filled values, as expected.
...
}
However, the AwesomeUser entity in the following test does not get any of the same properties auto-filled.
public class AwesomeUser : User {
...
}
...
[Theory, AutoDomainData]
public void SomeOtherTest(AwesomeUser user, ...) {
// user.Email nor user.CoolThings have auto-filled values. What gives?
...
}
In both test cases, the Id property is auto-filled because of my customization. If I remove my customization, the SomeOtherTest's AwesomeUser instance gets its inherited properties auto-filled just fine. I must assume that my customization is what is messing things up.
Is there a better way to get all my BaseEntity instances to set their Id, or is there something else I'm missing with AutoFixture? I've applied my customization first, in the middle, and last, to no avail.
The solution provided above is a pretty clever attempt, but not something I've seen before. A more idiomatic solution would be something like this:
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(
new FilteringSpecimenBuilder(
new Postprocessor(
new BaseEntityBuilder(
new ConstructorInvoker(
new ModestConstructorQuery())),
new AutoPropertiesCommand().Execute),
new BaseEntitySpecification()));
}
private class BaseEntityBuilder : ISpecimenBuilder
{
private readonly ISpecimenBuilder builder;
private readonly IRequestSpecification specification;
public BaseEntityBuilder(ISpecimenBuilder builder)
{
this.builder = builder;
this.specification = new BaseEntitySpecification();
}
public object Create(object request, ISpecimenContext context)
{
if (!this.specification.IsSatisfiedBy(request))
return new NoSpecimen(request);
var b = (BaseEntity)this.builder.Create(request, context);
b.SetId((Guid)context.Resolve(typeof(Guid)));
return b;
}
}
private class BaseEntitySpecification : IRequestSpecification
{
public bool IsSatisfiedBy(object request)
{
var t = request as Type;
if (t == null)
return false;
if (!typeof(BaseEntity).IsAssignableFrom(t))
return false;
return true;
}
}
As you can see, this isn't a simple one-liner, which is indicative of AutoFixture being a rather opinionated library. In this case, AutoFixture's opinion is:
Favor object composition over class inheritance.
-Design Patterns, p. 20
AutoFixture is first and foremost a TDD tool, and one of the main advantages of TDD is that it provides feedback about class design. In this case, the feedback is: Inheritance is awkward and troublesome. Reconsider the design.