Steve Luke wrote:Perhaps you show am example of the class you need to test. It isn't clear to me why you are having problems with final variables.
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// few import statements specific to my task
/**
* Handles user information and redirects based on user input validations.
*/
public class SignUpServlet extends HttpServlet {
/**
* Generated SerialVersionUID.
*/
private static final long serialVersionUID = 2303428554104148740L;
private transient final ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
"config/BeanLocations.xml");
private transient final ValidatorImpl validator = (ValidatorImpl) appContext.getBean("welcomeValidator");
/**
* {@inheritDoc} Receives user information and based on the input
* validations status, redirects to the respective page.
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
Map<
String, String[]> userInformation = new HashMap<String, String[]>();
userInformation = request.getParameterMap();
SimulatorDao simulator = (SimulatorDao) appContext.getBean("simulatorDao");
if ((isFormInValid("firstName", userInformation, request)
|| isFormInValid("lastName", userInformation, request)
|| isFormInValid("teamName", userInformation, request)
|| isFormInValid("userName", userInformation, request)
|| isFormInValid("password", userInformation, request)
|| isFormInValid("roleOptions", userInformation, request))
|| userNameAlreadyExists("userName", userInformation, request)) {
request.getRequestDispatcher("SignUpPage.jsp")
.forward(request, response);
return;
}
//checks whether the User information is successfully stored into database. If yes returns true.
if (UserInformationSetup.successfulRegistration(simulator,
userInformation)){
request.getRequestDispatcher("Success.jsp").forward(request,
response);
}
else{
request.getRequestDispatcher("ErrorPage.jsp")
.forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Returns boolean status for input value and sets error msg to request
* attribute.
*
* @param paramName Input field Name.
* @param userInformation User details entered through form.
* @param req Http request object.
* @return status returns false if validation is success, else returns true when validation is unsuccessful.
*/
private boolean isFormInValid(String paramName,
Map<String, String[]> userInformation, HttpServletRequest req) {
boolean status = false;
// does something - calling validation methods to validate and sets status to either true or false depending upon validation.
}
}
return status;
}
/**
* Checks if the User Name is already registered, if exists returns true and sets attribute, default is false.
*
* @param paramName User Name provided by user.
* @param userInformation User details entered through the form.
* @param requestObject Http request object
* @return returns false if name doesn't exist in the database, else returns true if name already registered/exists.
*/
private boolean userNameAlreadyExists(String paramName, Map<String, String[]> userInformation, HttpServletRequest requestObject){
boolean existenceStatus = false;
{
//calls the method which checks if name exists in database, if exists returns true else false.
}
return existenceStatus;
}
}
The concern that I have is, I am instantiating beans - simulator, validator through appContext(Using dependency injection). In order to test i need to mock appContext, simulator. But these are final and i cannot reassign values into these objects/instances.