mercredi 14 juin 2017

How to find out if a tag attribute has a specific value on a JSP page?

I'm new to a JSP and portlets. But I need to write tests wich can figure out has a tag's attribute called "name" a value in JSP page. Now I mock an ActionRequest object

private void setupCommunicationDamageForm() {
    when(request.getParameter("carBrand")).thenReturn("Audi");
    when(request.getParameter("carModel")).thenReturn("A7");
    when(request.getParameter("carRegisterNumber")).thenReturn("WI 12345");
    when(request.getParameter("carYear")).thenReturn("2017");
    when(request.getParameter("carVin")).thenReturn("1a2b3c4d5e6f7j8i9");
    when(request.getParameter("wasTowed")).thenReturn("false");
    when(request.getParameter("deductVatOpportunity")).thenReturn("false");
    when(request.getParameter("unrecoverableDamageBeforeEvent")).thenReturn("Rear bumper");
    when(request.getParameter("damagedVehicleLocation")).thenReturn("Warszawa, Centrum");
}

and test a method of Extractor class

public Car extractCarFromCommunicationDamageForm(ActionRequest request) {

    Car car = new Car();

    car.setBrand(ParamUtil.getString(request, "carBrand"));
    car.setModel(ParamUtil.getString(request, "carModel"));
    car.setRegisterNumber(ParamUtil.getString(request, "carRegisterNumber"));
    car.setYear(ParamUtil.getString(request, "carYear"));
    car.setVin(ParamUtil.getString(request, "carVin"));
    car.setWasTowed(ParamUtil.getBoolean(request, "wasTowed"));
    car.setHaveOpportunityToDeductVatOnRepairCosts(ParamUtil.getBoolean(request, "deductVatOpportunity"));
    car.setUnrecoverableDamageBeforeEvent(ParamUtil.getString(request, "unrecoverableDamageBeforeEvent"));
    car.setDamagedVehicleLocation(ParamUtil.getString(request, "damagedVehicleLocation"));

    return car;
}

But when I change a value of the "name" attribute in a JSP page from "carBrand" to "brand", my test still will be fine, 'cuz ActionRequest will return "carBrand" value as

public Car extractCarFromCommunicationDamageForm(ActionRequest request) 

method wants. How can I protect myself from accidentally changing the value of an attribute on a page? Thanks in advance)

Aucun commentaire:

Enregistrer un commentaire