Hi, I am new to EJB3 and am unsure if this is possible, any help would be greatly appreciated.
Say I have a public interface MyInterface which has some methods and a string:
public final static String JNDI_NAME = ".../ejb/MySlsb/remote"
And then I have a slsb MySlsb that implements MyInterface and has a string:
public final static String NAME = "MySlsb"
now, I have a test class that is running outside the container. in this test i can instantiate a MyInterface by:
InitialContext ctx = new InitialContext(); MyInterface myInterface = (MyInterface) ctx.lookup(MyInterface.JNDI_NAME);
however, i would like to implement some methods in MySlsb that i don't want to expose in MyInterface, so, in order to call these methods i need to instantiate a MySlsb object, not a MyInterface object. (simply casting MyInterface object to MySlsb does not work, this throws a class cast exception at runtime, having something to do with wrapper classes...)
is there anyway to instantiate the MySlsb object?
[ October 16, 2008: Message edited by: Johann Dobbins ] [ October 16, 2008: Message edited by: Johann Dobbins ]
The way to do this right now would be to have another interface specific for testing. You can deploy this interface via a deployment descriptor without changing the actual EJB implementation and use the descriptor only for testing purposes.
If you don't need container services anyway, you can just instantiate the bean class with "new". That's often the case with internal unit test cases which is what I suspect you are talking about. Otherwise, why have a method that's not client invokable :-)
P.S.: In EJB 3.1, interfaces are optional, so if you really want to, you can look up the bean class directly. All public methods are automatically invokable.
Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1