84398

Testing ServiceLocator using JUnit

Question:

This is a follow up question to my <a href="https://stackoverflow.com/questions/1698212/writing-test-case-for-a-dao-on-a-j2ee-application" rel="nofollow">previous question</a>.

I am trying to write test case for my ServiceLocator class but it gives me the following error:

com/iplanet/ias/admin/common/ASException java.lang.NoClassDefFoundError: com/iplanet/ias/admin/common/ASException at java.lang.ClassLoader.defineClass1(Native Method)

My test case:

public void testServiceLocator () throws UivException, NamingException { DataSource ds = ServiceLocator.getInstance().getDataSource("jdbc/RSRC/my/mydb"); //I have not put any assert statements because i know above line is failing }

The above code fails on getInstance() method which looks like this:

static public ServiceLocator getInstance() throws UivException { try { if (me == null) { synchronized(ServiceLocator.class) { me = new ServiceLocator(); } } return me; } catch (UivException e) { throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR, ErrorCode.SERVICE_LOCATOR_LOOKUP_ERROR, e.getMessage()); } }

I know this ServiceLocator works fine because when I test my application from the front end there are no issues. Only reason I am writing this test case is because I want to test my <a href="http://en.wikipedia.org/wiki/Data_access_object" rel="nofollow">DAO</a>. And for me to test my DAO ServiceLocator has to work (from <a href="http://en.wikipedia.org/wiki/JUnit" rel="nofollow">JUnit</a>).

I don't have a clue what to make of the error message. Does anyone want to suggest something I can try that will hopefully work?

EDIT: ServiceLocator constructor

private ServiceLocator() throws UivException { try { ic = new InitialContext(); // System.out.println("Created the Initial Context"); cache = Collections.synchronizedMap(new HashMap()); } catch (NamingException ne) { throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR, 0, ne.getMessage()); } catch (NullPointerException e) { throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR, 0, e.getMessage()); } }

Answer1:

Well, is com.iplanet.ias.admin.common.ASException on the classpath when you invoke your tests?

Are you relying on the app server to have the JDBC driver's libraries in it's classpath, or are you deploying it yourself?

Answer2:

Actually, the error is pretty clear: java.lang.NoClassDefFoundError: com/iplanet/ias/admin/common/ASException indicates that the definition of ASException cannot be found at runtime.

When running in the application server, this class is provided by iPlanet Application Server and the code just runs fine. To run this code outside the application server context, you'll have to provide it "manually" by putting it to the classpath. So you have to add the right iPlanet JAR to the classpath (this is the tricky part, you'll have to find which one).

Additionally, I can see taht you're using the InitialContext non-arg constructor so you're using iPlanet's environment settings when running inside the application server. Outside iPlanet (e.g. for a unit test), you'll have to provide the right JNDI properties for iPlanet yourself. To do so, you'll have to put a jndi.properties file on the classpath with (at least) the initial context factory and the provider URL. Something like this:

java.naming.factory.initial=... java.naming.provider.url=...

Check your iPlanet documentation for the values.

Answer3:

I'm curious why you're writing a service locator. Is it part of an assignment? In the real world, Dependency Injection is the clearly superior choice, using either Guice or Spring.

Recommend

  • Posting to Facebook fan page feed as user through PHP SDK
  • Choose return class type based on parameter with Java 7
  • Continue Postgres transaction with exceptions in Java
  • Can I create a MYSQL table using a PDO parameterized statement?
  • Read-only file system
  • Why is synchronized not working?
  • Unable to open phpmyadmin directory: Call to undefined function
  • How to implement TCP connection pooling in java?
  • How can i extract image from button icon in PDF using Apache PDFBox?
  • How do correctly use Jackson Mixin annotation to instantiate a third party class?
  • Java Mail: Unable to send email via Yahoo
  • Automatically Catch Exceptions for PDO in PHP
  • Coalescing items in channel
  • Populating Spinner with JSON
  • java rmi deadlock
  • Android: save file to downloads that can be viewed later
  • Turning String textfile into object array
  • Spring bean validation messages resolution
  • Powerpoint 2010 VSTO AddIn taskpane on multiple Windows
  • oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Invalid JWT
  • Convert C# statements including NSubstitute code to VB.net
  • Generic classes with Collection getter of other types
  • Ionic storage “get” returns null only on the second call within method
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • PDO error when wrong host name
  • How to copy styled text in JTextPane
  • Why does it draw lines in the wrong place?
  • Cloud Code function running twice
  • Thread safety of a fluent like class using clone() and non final fields
  • Converting a WriteableBitmap image ToArray in UWP
  • Record samples being played with OpenAL
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Why value captured by reference in lambda is broken? [duplicate]
  • javascript inside java/jsp code
  • Bitwise OR returns boolean when one of operands is nil
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?