Create a subclass of your servlet for your tests, where you set these values (in your Java code). Something like (using a Map so the pattern is easily reusable when you have more than one such parameter):
public class MyServletForTests extends MyServlet {
private static final Map<String, String> INIT_PARAMETERS;
static {
Map<String, String> initParameters = new HashMap<String,String>();
initParameters.put("cacheServiceLocatorClass", "com.myco.clearing.product.test.MockCacheServiceLocator");
INIT_PARAMETERS = Collections.unmodifiableMap(initParameters);
}
@Override
public void init(final ServletConfig config) {
super.init(new ServletConfig() {
public String getServletName() { return config.getServletName(); }
public ServletContext getServletContext() { return config.getServletContext(); }
public String getInitParameter(String name) {
// you could also check if the map contains the key and fallback to the super implementation otherwise; it would complicate getInitParameterNames though
return INIT_PARAMETERS.get(name);
}
public Enumeration<String> getInitParameterNames() {
return Collections.enumeration(INIT_PARAMETERS.keySet());
}
}
}
}