Search code examples
javaspringjunitjndi

Is there a way for creating a spring context which can run before all of my junit unit tests which are on different classes?


Sorry I did not research this issue deeply on the internet or on SO. So down voting is welcome. After all I have a feeling this may be a dump question. However I am giving a shot.

I have a project which will contain all of the unit tests. All of the unit tests require a setup which basically creates a jndi namespace. I am doing this with @Before tag on each of my unit test class. So I am doing lots of copy paste which is irritating me a little. Is there a way of creating a singleton class which creates this jndi namespace for all of my unit tests possibly with spring or any other way.


Solution

  • Thanks for the M. Deinum sollution. If he posted the answer I would accept it.

    I created a master class like this

    import java.io.IOException;
    import java.sql.SQLException;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.naming.NamingException;
    
    public class JndiInit{
    
        @PostConstruct
        public void init() throws IOException, SQLException, NamingException{
            System.out.println("Mastersetup for initializing jndi namespace");
        }
    
        @PreDestroy
        public void tearDown(){
            System.out.println("Tear down");
        }
    
    }
    

    My unit tests uses a spring context Test 1 :

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("/testContext.xml")
    public class Test1{
    
    
        @Test
        public void test(){
            System.out.println("test 1");
        }
    
    }
    

    And Test2

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("/testContext.xml")
    public class Test2 {
    
        @Test
        public void test(){
            System.out.println("test 2");
        }
    }
    

    And my spring context is like :

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean class="tr.com.mhrs.test.base.jndifactory.JndiInit">
    
        </bean>
    </beans>
    

    It successfully achieved what I want.

    Mastersetup for initializing jndi namespace
    test 1
    test 2
    Tear down
    

    Thanks guys.