basith
basith

Reputation: 141

Use context parameters in junit test case

In Spring,how to use context paramters in the tomcat location conf/context.xml in the junit testing or how to create context parameters in junit

<Context>
    <Parameter name="mail_host" value="smtp.gmail.com" override="true"/>
</Context>

I am implementing the above parameters in my code as i specified below
@Value("#{contextParameters.mail_host}").It is working fine when i am using tomcat.

But i am getting an error 'Field or property 'contextParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' when running in junit

Please help me,I am struggling in this point.Thanks in advance

Upvotes: 0

Views: 2300

Answers (2)

Flamboyaa
Flamboyaa

Reputation: 36

One solution that I've used is to stub the context parameters in my test spring applicationContext file with a simple properties bean. So in your case:

<util:properties id="contextParameters"> <prop key="mail_host">smtp.gmail.com</prop> </util:properties>

This is the easiest way to handle it. Hope it helps!

Upvotes: 0

kyla
kyla

Reputation: 396

You could construct JNDI before you run your test and in your test you can retrieve the information from context. Take a look a this example how he simulates injecting datasource using jndi here. And your context would look something like this(in actual tomcat)

<Resource name="jdbc/TestDB"
          auth="Container"
          type="javax.sql.DataSource"
          username="root"
          password="password"
          driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/mysql"/>

So now we want to simulate this as if we would retrieve it using JNDI. So in JUnit test cast we would like to setup Before class.Please note that not all information is shown here(constructing the datasource in @Before), but you got the point.

@BeforeClass
    public static void setUpClass() throws Exception {
        // rcarver - setup the jndi context and the datasource
        try {
            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES, 
                "org.apache.naming");            
            InitialContext ic = new InitialContext();

            ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");

            // Construct DataSource
            OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
            ds.setURL("jdbc:oracle:thin:@host:port:db");
            ds.setUser("MY_USER_NAME");
            ds.setPassword("MY_USER_PASSWORD");

            ic.bind("java:/comp/env/jdbc/nameofmyjdbcresource", ds);
        } catch (NamingException ex) {
            Logger.getLogger(MyDAOTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

and in your test you can get this information

Context initContext = new InitialContext();
        Context webContext = (Context)initContext.lookup("java:/comp/env");

        DataSource ds = (DataSource) webContext.lookup("jdbc/nameofmyjdbcresource");

Hope it helps. EDIT e.g.

MyMailHost mailHost = new MyMailHost();
mailHost.setName("mail_host");
mailHost.setValue("smtp.gmail.com");
mailHost.setOverride(true);

 ic.bind("java:/comp/env/jdbc/mymailhost", mailHost);

in @Test
Context initContext = new InitialContext();
            Context webContext = (Context)initContext.lookup("java:/comp/env");

            MyMailHost ds = (MyMailHost) webContext.lookup("jdbc/mymailhost");

Upvotes: 1

Related Questions