0

I'm trying to test a webapp using Junit. The class I want to test CalculMoisUtils load a bean from applicationcontext ( spring) using this code:

Properties prop = new Properties();
InputStream input = null;
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
String file = (String) ctx.getBean("CalendarProp");
input = new FileInputStream(file);
prop.load(input);

Here is the test class :

package test.webapp.utils;

    import static org.junit.Assert.*;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;


    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    import com.logica.planchaweb.utils.CalculMoisUtils;

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({"file:src/applicationcontext.xml","file:src/applicationcontext-security.xml" })
    public class CalculMoisUtilsTest {

        ClassPathXmlApplicationContext ctx;

        @Before
        public void setUp(){
             ctx = new ClassPathXmlApplicationContext("file:src/applicationcontext.xml");
        }

        @Test
        public void test() {

            Properties prop = new Properties();
            InputStream input = null;
            try {
                String file = (String) ctx.getBean("CalendarProp");
                input = new FileInputStream(file);
                prop.load(input);
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }finally{
                try{
                    input.close();
                }catch(Exception e){}
            }

            CalculMoisUtils calculMoisUtils = new CalculMoisUtils();
        }
    }

The problem is that the class I want to test doesn't have the applicationcontext even if I load it on the test class, where the same code works.

Thanks for your help

Romain

  • I'm confused; you're `new`ing up the bean you want to test instead of getting it from the context? – Dave Newton Jul 09 '14 at 15:38
  • The class I want to test is not in the context but it uses parameters which are in the context – user3821094 Jul 09 '14 at 15:51
  • If you `new` up a class that uses a Spring context, and that class doesn't do its own Spring context init, it'll be completely ignorant of anything Spring-related. Without bytecode manipulation, `new` completely bypasses all Spring-related things. – Dave Newton Jul 09 '14 at 16:19
  • And what if I don't want to change my class code? Is there any way to "inject" the context in the class I want to test? – user3821094 Jul 10 '14 at 07:29
  • Sure, if it had a mechanism for taking a context, or you want to manipulate the class byte code-there's a project that allows "new" to respect Spring configuration but I don't recall the name. Or you could just instantiate it via Spring, which seems like the obvious choice, since that must be how your app does it. – Dave Newton Jul 10 '14 at 10:09
  • Thank you for your help I'll try to find the project you're talking about. If I find it I'll post the name here – user3821094 Jul 16 '14 at 09:49

0 Answers0