How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

fl4l

I have an application in spring with database oracle running on JBOSS 7.1. I want to test my service layer beans simply running a junit test. In my spring context I use a jndi datasource like this:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jboss/datasources/myDatasource" />
    <property name="resourceRef" value="true"/>
</bean>

When I run my junit test that loads the spring context test I receive an exception like:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [context.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)

How can I inject the JNDI datasource in my tests without change my context in jboss?

fl4l

According with this post, or this great blog post I found three ways to solve my issue, just create BeforeClass method in your JUnitTest Class.

I post it for the community:

- Solution 1
This solution, requires the catalina.jar and the oracledriver in your classpath:

@BeforeClass
public static void setUpClass() throws Exception {
    try {
        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("jboss");
        ic.createSubcontext("jboss/datasources");
        ic.createSubcontext("jboss/datasources/myDatasource");

        OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
        ds.setURL("jdbc:oracle:thin:@xxxxx:1521:xxxxx");
        ds.setUser("myUserid");
        ds.setPassword("myPass");

        ic.rebind("jboss/datasources/myDatasource", ds);
    } catch (NamingException ex) {
        ex.printStackTrace();
    }
}

if you use maven you can put in your pom:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>catalina</artifactId>
    <version>6.0.37</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3.0</version>
    <scope>test</scope>
</dependency> 


- Solution 2
This solution, requires commons-dbcp in your classpath:

@BeforeClass
public static void setUpDataSource() throws Exception {
    try {
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        cpds.setDriver("oracle.jdbc.OracleDriver");
        cpds.setUrl("jdbc:oracle:thin:@xxxxx:1521:xxxxx");
        cpds.setUser("myUsername");
        cpds.setPassword("myPass");

        SharedPoolDataSource dataSource = new SharedPoolDataSource();
        dataSource.setConnectionPoolDataSource(cpds);
        dataSource.setMaxActive(10);
        dataSource.setMaxWait(50);
        builder.bind("jboss/datasources/myDatasource", dataSource);
        builder.activate();
    } catch (NamingException ex) {
        ex.printStackTrace();
    }
}

in your pom:

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
    <scope>test</scope>
</dependency>


- Solution 3
This solution uses the OracleConnectionPoolDataSource included in Oracle Driver:

@BeforeClass
public static void setUpDataSource() throws Exception {
    try {
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();

        OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
        ds.setURL("jdbc:oracle:thin:@xxxxx:1521:xxxxx");
        ds.setUser("myUsername");
        ds.setPassword("myPass");           

        builder.bind("jboss/datasources/myDatasource", ds);
        builder.activate();
    } catch (NamingException ex) {
        ex.printStackTrace();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

From Dev

Multiple Spring datasources for JUnit test

From Dev

How to get jndi environment variables with junit test in Spring?

From Dev

How to get jndi environment variables with junit test in Spring?

From Dev

How to run Spring Shell scripts in a JUnit test

From Dev

Injecting logic between @Before and @Test execution with Spring and JUnit

From Dev

jndi-name property override - JUnit - Spring - Struts Integration test

From Dev

How to run JUnit test in Eclipse

From Dev

How to test ConfigurationProperties in Spring with JUnit?

From Dev

how can I use my dynamic JNDI datasources with JPA?

From Dev

How to run test methods in order with Junit

From Dev

How to run a single JUnit test method in Eclipse?

From Dev

How to run jUnit test in specific folder by gradle?

From Dev

How to run all modified JUnit test classes?

From Dev

How to run a specific junit test in ant with parameter?

From Dev

How to run parallel test jUnit5 in spring boot - cucumber version 5 and more

From Dev

How to run JUnit 5 and JUnit 4 test suites in Gradle?

From Java

How to make Junit test in spring boot

From Dev

How to write JUnit test with Spring Autowire?

From Dev

What happens internally when I "Run Spring as JUnit Test"?

From Dev

Spring-boot - How to load resources/datasources?

From Dev

TransactionManager from Spring or JNDI? (JBOSS + Spring 3 + Hibernate 4 + JTA)

From Dev

Spring junit test not working

From Dev

How to run a simple JUnit4 test in Android Studio 1.1?

From Dev

How to run only one junit test case in maven?

From Dev

Eclipse: How to run a single inherited JUnit test method

From Dev

How do I run JUnit 4.11 test cases with SBT?

From Dev

How do I run a junit test only after the build?

From Dev

How to run JUnit test cases in play framework 2.3.2

Related Related

  1. 1

    How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

  2. 2

    Multiple Spring datasources for JUnit test

  3. 3

    How to get jndi environment variables with junit test in Spring?

  4. 4

    How to get jndi environment variables with junit test in Spring?

  5. 5

    How to run Spring Shell scripts in a JUnit test

  6. 6

    Injecting logic between @Before and @Test execution with Spring and JUnit

  7. 7

    jndi-name property override - JUnit - Spring - Struts Integration test

  8. 8

    How to run JUnit test in Eclipse

  9. 9

    How to test ConfigurationProperties in Spring with JUnit?

  10. 10

    how can I use my dynamic JNDI datasources with JPA?

  11. 11

    How to run test methods in order with Junit

  12. 12

    How to run a single JUnit test method in Eclipse?

  13. 13

    How to run jUnit test in specific folder by gradle?

  14. 14

    How to run all modified JUnit test classes?

  15. 15

    How to run a specific junit test in ant with parameter?

  16. 16

    How to run parallel test jUnit5 in spring boot - cucumber version 5 and more

  17. 17

    How to run JUnit 5 and JUnit 4 test suites in Gradle?

  18. 18

    How to make Junit test in spring boot

  19. 19

    How to write JUnit test with Spring Autowire?

  20. 20

    What happens internally when I "Run Spring as JUnit Test"?

  21. 21

    Spring-boot - How to load resources/datasources?

  22. 22

    TransactionManager from Spring or JNDI? (JBOSS + Spring 3 + Hibernate 4 + JTA)

  23. 23

    Spring junit test not working

  24. 24

    How to run a simple JUnit4 test in Android Studio 1.1?

  25. 25

    How to run only one junit test case in maven?

  26. 26

    Eclipse: How to run a single inherited JUnit test method

  27. 27

    How do I run JUnit 4.11 test cases with SBT?

  28. 28

    How do I run a junit test only after the build?

  29. 29

    How to run JUnit test cases in play framework 2.3.2

HotTag

Archive