0

OpenLiberty is running in dev-mode. Somewhere in my code i use

Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("app/myDB");

with a datasource configured in the webserver:

<dataSource id="mssql" jndiName="app/myDB">
    <connectionManager maxPoolSize="20000" minPoolSize="2"/>

    <jdbcDriver libraryRef="MSSQL"/>
    <properties.microsoft.sqlserver databaseName="myDB" serverName="xxx.xxx.xxx.xxx" portNumber="1433" user="X" password="Y"/>
</dataSource>

This works fine so far, until i try to run my units tests.

When trying to run the tests on demand, that somewhere call this code:

Context initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("app/myDB");

my webapp fails with

javax.naming.NoInitialContextException

So why do the unit tests do not have the initial context? Can i somehow tell OpenLiberty to provide that resource for test scope? Or do i have to mock the initialContext? I am a beginner in writing unit/integration test. If i need to provide additional information please tell me.

edit: added dev-mode and general clarification

kanonroald
  • 41
  • 6

1 Answers1

0

In order for a DataSource to be made available for JNDI lookups in Liberty, you need to have the Liberty server running and the jndi-1.0 feature enabled, and one of the jdbc-4.x features enabled. For example, in server.xml,

<<server>
    <featureManager>
        <feature>jndi-1.0</feature>
        <feature>jdbc-4.2</feature>
        ... other features
    </featureManager>

    ...
</server>
njr
  • 3,399
  • 9
  • 7
  • I edited my qeustion a bit. Should this also work for the unit tests? In the normal context of my application e.g. clicking a button or whatever on a page, the jndi lookup does work. Only in the context of unit tests (when running openliberty in dev mode) do not work – kanonroald Mar 04 '22 at 14:10
  • As long as you have the Liberty server up and running with the features listed above, JNDI lookups should work. Here's an example of working automated test bucket which is doing this: https://github.com/OpenLiberty/open-liberty/tree/integration/dev/com.ibm.ws.concurrent_fat_db While it is named as a Functional Acceptance Test bucket, you could use the same or a similar approach for unit tests. – njr Mar 04 '22 at 14:15
  • I posted the wrong test bucket link above (that one didn't enable the jndi-1.0 feature) but stackoverflow doesn't allow editing the comment to correct it. Here is a link to a test bucket that does actually have jndi-1.0 enabled: https://github.com/OpenLiberty/open-liberty/tree/integration/dev/com.ibm.ws.jdbc_fat_derby – njr Mar 04 '22 at 14:24
  • Is your unit test running inside the Liberty server, or is your unit test running in a separate JVM instance? – Alasdair Mar 04 '22 at 14:53
  • Thank you for your comments. I am trying to get this to work with the 2nd link right now, but with no success as of now. I activated the features and get this error now: "javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial". I am not sure if its the same JVM. I started the webserver with the maven liberty plugin in dev mode and hit ENTER to run the tests. – kanonroald Mar 04 '22 at 14:59
  • can i somehow force junit to run in the same jvm? – kanonroald Mar 04 '22 at 16:05
  • @njr So i am trying to get this to work again with the help of this repository https://github.com/OpenLiberty/open-liberty/tree/integration/dev/com.ibm.ws.jdbc_fat_derby. My DataSource is like ds8 in that case. I set up my server.xml as in the example. I didnt include an entry in the web.xml, as ds8 doesnt have one aswell. Now my next question is: Do i need the FATSuite class? Or what is it for, i dont understand that part? I am using maven as the build tool, and included maven-war-plugin, maven-surefire-plugin and maven-falsafe-plugin. But its still not working. Do i need something else? – kanonroald Mar 07 '22 at 12:36
  • FATSuite.java is there to get JUnit to build the test application and export it to the Liberty server, so it's only peripheral and you can ignore that part because you are using maven to build the test app. The important piece is to have the tests running in the Liberty server where the JNDI service will be available. – njr Mar 07 '22 at 16:49
  • Alright. So what am I doing wrong that my tests do not run inside the webserver? The tests are under src/test/.... . I am running the webserver in DEV mode with the Maven Liberty Plugin. I press ENTER to run the "tests on demand" Do you have an idea whats wrong with my setup? – kanonroald Mar 07 '22 at 21:45