SeamTest - It's not @In-jecting bean attributes
bsmithjj Aug 16, 2006 2:08 PMHello,
I have a Stateless Session Bean - here is it's interface and the implementation:
@Local @Remote public interface UserEditor { public String createUser(); public String deleteUser(); public String updateUser(); public String loadUser(); public void setUser(User user); public User getUser(); public void setUsername(String username); }
@Stateless @Scope(ScopeType.STATELESS) @Name("userEditor") public class UserEditorBean implements UserEditor { @Logger private Log log; private User user; private String username; private EntityManager em; public UserEditorBean() { // troubleshooting System.out.println("\n new UserEditorBean() \n"); } public String createUser() { try { user.initDates(); em.persist(user); return "success"; } catch (Exception e) { e.printStackTrace(); return null; } } public String deleteUser() { return null; } public String updateUser() { return null; } public String loadUser() { return null; } @In public void setUser(User user) { this.user = user; log.info("setUser() : user -> "+user); } @Out public User getUser() { return this.user; } @In(required = false) public void setUsername(String username) { this.username = username; } @In public void setEm(EntityManager em) { this.em = em; log.info("setEm (entity manager)"); } }
Here also is the entity this bean will operate on:
@Entity @Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = {"username"})} ) @Name("user") public class User extends BaseTimestampedEntity implements java.io.Serializable { private static final long serialVersionUID = -2717302256558315283L; private String username; private boolean enabled; public User() {System.out.println("\n new User() \n");} @Column(name = "username", unique = true, nullable = false, insertable = true, updatable = true, length = 255) public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } @Column public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public String toString() { final StringBuilder sb = new StringBuilder(); sb.append("User"); sb.append("{id=").append(id); sb.append(", username='").append(username).append('\''); sb.append(", enabled=").append(enabled); sb.append(", createDate=").append(createDate); sb.append(", lastUpdate=").append(lastUpdate); sb.append('}'); return sb.toString(); } }
Here is my SeamTest:
public class UserEditorBeanTest extends SeamTest { Log log = LogFactory.getLog(UserEditorBeanTest.class); @Test public void test_createUser_deleteUser() throws Exception { new Script() { UserEditor userEditor; @Override protected void updateModelValues() { log.info("updateModelValues()"); User user = (User)getInstance("user"); assert user != null; user.setUsername("Test-Username"); user.setEnabled(true); log.info("user -> "+user); userEditor = (UserEditor) Component.getInstance("userEditor"); } @Override protected void invokeApplication() { log.info("invokeApplication()"); String outcome = userEditor.createUser(); log.info("outcome = "+outcome); assert "success".equals(outcome); userEditor.deleteUser(); } }.run(); } }
The problem I am having is that when the SeamTest.Script runs, it is not injecting component(s) onto the UserEditorBean. I am getting a NullPointerException on the first line of the createUser() method where I assume the user has been set and I attempt to initDates(). I've tried putting the code to obtain a "userEditor" component in different methods of Script ( setup(), etc....), but always the NullPointerException occurs.
Here is a log of the exception that occurs during the test run:
14:05:20,217 DEBUG org.jboss.seam.jsf.AbstractSeamPhaseListener.(restoreAnyConversationContext:52) - After restoring con versation context: ConversationContext(1) 14:05:20,217 DEBUG org.jboss.seam.jsf.SeamPhaseListener.(beforePhase:40) - before phase: APPLY_REQUEST_VALUES 2 14:05:20,217 DEBUG org.jboss.seam.jsf.SeamPhaseListener.(afterPhase:57) - after phase: APPLY_REQUEST_VALUES 2 14:05:20,217 DEBUG org.jboss.seam.jsf.SeamPhaseListener.(beforePhase:40) - before phase: PROCESS_VALIDATIONS 3 14:05:20,217 DEBUG org.jboss.seam.jsf.SeamPhaseListener.(afterPhase:57) - after phase: PROCESS_VALIDATIONS 3 14:05:20,217 DEBUG org.jboss.seam.jsf.SeamPhaseListener.(beforePhase:40) - before phase: UPDATE_MODEL_VALUES 4 14:05:20,217 INFO com.evergreen.UserEditorBeanTest.(updateModelValues:30) - updateModelValues() 14:05:20,217 DEBUG org.jboss.seam.Component.(newInstance:727) - instantiating Seam component: user new User() 14:05:20,233 INFO com.evergreen.UserEditorBeanTest.(updateModelValues:35) - user -> User{id=0, username='Test-Username' , enabled=true, createDate=null, lastUpdate=null} 14:05:20,233 DEBUG org.jboss.seam.Component.(newInstance:727) - instantiating Seam component: userEditor 14:05:20,233 DEBUG org.jboss.seam.util.Naming.(getInitialContext:24) - JNDI InitialContext properties:{java.naming.facto ry.initial=org.jnp.interfaces.LocalOnlyContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces} 14:05:20,233 DEBUG org.jboss.seam.jsf.SeamPhaseListener.(afterPhase:57) - after phase: UPDATE_MODEL_VALUES 4 14:05:20,233 DEBUG org.jboss.seam.jsf.SeamPhaseListener.(beforePhase:40) - before phase: INVOKE_APPLICATION 5 14:05:20,233 INFO com.evergreen.UserEditorBeanTest.(invokeApplication:43) - invokeApplication() 14:05:20,264 DEBUG org.jboss.security.SecurityAssociation.(<clinit>:143) - Using ThreadLocal: false new UserEditorBean() java.lang.NullPointerException at com.evergreen.impl.UserEditorBean.createUser(UserEditorBean.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112) at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166) at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerIntercep tor.java:54) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79) at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106) at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101) at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:181) at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:79) at $Proxy36.createUser(Unknown Source) at com.evergreen.UserEditorBeanTest$1.invokeApplication(UserEditorBeanTest.java:45) at org.jboss.seam.mock.SeamTest$Script.run(SeamTest.java:242) at com.evergreen.UserEditorBeanTest.test_createUser_deleteUser(UserEditorBeanTest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:552) at org.testng.internal.Invoker.invokeMethod(Invoker.java:407) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:778) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105) at org.testng.TestRunner.privateRun(TestRunner.java:682) at org.testng.TestRunner.run(TestRunner.java:566) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:220) at org.testng.SuiteRunner.run(SuiteRunner.java:146) at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:713) at org.testng.TestNG.runSuitesLocally(TestNG.java:676) at org.apache.maven.surefire.testng.TestNGExecutor.executeTestNG(TestNGExecutor.java:64) at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75) at org.apache.maven.surefire.Surefire.run(Surefire.java:129) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:225) at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:747) 14:05:20,326 INFO com.evergreen.UserEditorBeanTest.(invokeApplication:46) - outcome = null 14:05:20,326 DEBUG org.jboss.seam.contexts.Lifecycle.(endSession:173) - End of session, destroying contexts
So I can see that the components are being created, however, it appears that the UserEditorBean is not having a User @In-jected into it.
Help?
Thanks