struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Miguel Almeida <mig...@almeida.at>
Subject RE: Type conversion on a Map
Date Fri, 21 Sep 2012 14:34:49 GMT
On Fri, 2012-09-21 at 09:04 -0400, Martin Gainty wrote:

> a stupid question but i have to ask

If you mean "where is the initialisation of simpleMap e.g. simpleMap=new
java.util.HashMap();" - it's not there. I don't think you need to,
Struts initializes it. (see below)

> 
> public class TypeConversionAction extends ActionSupport 
> {
>     private Map<Entity, String> simpleMap;   //where is the initialisation of simpleMap
e.g. simpleMap=new java.util.HashMap();
>     public Map<Entity, String> getSimpleMap()  { return simpleMap;  }
>     public void setSimpleMap(Map<Entity, String> simpleMap) {   this.simpleMap
= simpleMap; }
>     public String execute() {  return SUCCESS; }
> }
> 
> //then the test-harness:
> 
> package test.example;
> import java.util.Map;
> import org.apache.struts2.StrutsTestCase;
> import org.junit.Test;
> import com.opensymphony.xwork2.ActionProxy;
> import com.opensymphony.xwork2.ActionSupport;
> 
> public class TypeConversionTest extends StrutsTestCase 
> {
> private ActionProxy actionProxy;    
> private ActionSupport action;
> 
> @Test
> public void test() throws Exception{
>     Entity entity = new Entity();
>     entity.setId(1);
>     super.setUp();
> 
>     request.setParameter("simpleMap[1]", "value");
>     createAction("/example/Convesion.action");        
>     executeProxy();
> 
>     Map<Entity, String> complexMap = ((TypeConversionAction) action).getSimpleMap();
>     assertNotNull(complexMap);   //getSimpleMap is NULL in the Action so it would always
be NULL here

No. The test fails below, not here. executeProxy executed the action and
initialized the map. If you run the test you'll see it fails on the last
assertEquals, not here.

>         
>     assertFalse(complexMap.isEmpty());  //FAIL it is empty
>     assertEquals("value", complexMap.get(entity));     //Never initialised so this would
fail as well

This is where it is failing. Not because it hasn't been initialized, but
because it doesn't have that key.

Miguel Almeida

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message