Skip to content

Instantly share code, notes, and snippets.

@albans
Created June 18, 2012 13:21
Show Gist options
  • Save albans/2948345 to your computer and use it in GitHub Desktop.
Save albans/2948345 to your computer and use it in GitHub Desktop.
Helper class for testing struts2 actions. The static block does the minimum wiring to avoid getText to crash. The inject method is helpful for injecting parameters to the tested action.
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts2.ServletActionContext;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.util.Assert;
import uk.ltd.getahead.dwr.util.Logger;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.util.ValueStack;
import eu.ec.eso.varo.action.ActionBase;
/**
* Helper class for testing struts2 actions. The static block does the minimum wiring
* to avoid getText to crash. The inject method is helpful for injecting parameters
* to the tested action
*/
@SuppressWarnings("unchecked")
public class ActionTestHelper {
private final static Logger log = Logger.getLogger(ActionTestHelper.class);
static {
ActionContext actionContext = mock(ActionContext.class);
ServletContext servletContext = mock(ServletContext.class);
when(actionContext.getLocale()).thenReturn(Locale.FRENCH);
ValueStack valueStack = mock(ValueStack.class);
Map<String, Object> context = new HashMap<String,Object>();
Container container = mock(Container.class);
XWorkConverter conv = mock(XWorkConverter.class);
when(container.getInstance(XWorkConverter.class)).thenReturn(conv);
when(conv.convertValue(any(Map.class), any(Object.class), any(Class.class))).thenAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
log.info(invocation.getArguments()[1].toString());
return "VALUE";
}
});
context.put(ActionContext.CONTAINER, container);
when(valueStack.getContext()).thenReturn(context);
when(actionContext.getValueStack()).thenReturn(valueStack);
ServletActionContext.setContext(actionContext);
ServletActionContext.setServletContext(servletContext);
}
public static void inject(ActionBase action, Object... args) throws IllegalAccessException, InvocationTargetException {
Assert.notNull(action);
Assert.notNull(args);
Assert.notEmpty(args);
Assert.isTrue(args.length%2==0, "You should have an even number of arguments after the action.");
for (int i=0; i<args.length; i=i+2) {
Assert.isTrue(String.class.isInstance(args[i]), String.format("Argument %1$d should be a string", i));
BeanUtils.setProperty(action, (String)args[i], args[i+1]);
}
}
}
@franciscodominguez
Copy link

where is the code for the eu.ec.eso.varo.action.ActionBase?

@Nullpo
Copy link

Nullpo commented Oct 9, 2013

Excelente idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment