Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring injecting an interface implementation gives Nullpointer
    primarykey
    data
    text
    <p>I am very new to Spring and Spring security!</p> <p>After following some tutorials I am now trying to implement Spring Security into our application. The problem I am having is that I want to test my class <code>UserDetailsImpl</code> with stubs. Therefor I would like Spring to inject 2 objects as stubs that I have created (they implement from an interface). This way, when I run my test, the stubs will get implemented, but during production code, the normal real implementations will be injected. unfortunately I get a <code>NullPointerException</code>. This is my code:</p> <p>The class that has to be injected:</p> <pre><code>package com.somePackageName.security; imports... @Component public class UserDetailsImpl implements UserDetails { @Autowired IUserFactory userFactory; public void setUserFactory(IUserFactory userFactory){ this.userFactory = userFactory; } @Autowired IUser user; public void setUser(IUser user){ this.user = user; } </code></pre> <p>IUser &amp; IUserFactory are interfaces.</p> <p>This is the <code>IUserFactory</code> stub that I want to implement:</p> <pre><code>package com.somePackageName; import com.su.rbac.virtuoso.IUser; import com.su.rbac.virtuoso.IUserFactory; public class StubUserFactory implements IUserFactory { public StubUserFactory(){} @Override public IUser findUserWithUsername(String username) { return new StubUser(); } @Override public IUser createUser(String username, String password) { return new StubUser(); } } </code></pre> <p>And finally my <code>application-context.xml</code> file:</p> <pre><code>&lt;global-method-security pre-post-annotations="enabled"/&gt; &lt;context:annotation-config /&gt; &lt;context:component-scan base-package="com.somePackageName" /&gt; &lt;beans:bean id="userDetailsImpl" class="com.somePackageName.security.UserDetailsImpl"&gt; &lt;!-- &lt;beans:property name="userFactory" value="com.somePackageName.StubUserFactory"/&gt; &lt;beans:property name="user" value="com.somePackageName.StubUser"/&gt; --&gt; &lt;beans:property name="userFactory" ref="StubUserFactory"/&gt; &lt;beans:property name="user" ref="StubUser"/&gt; &lt;/beans:bean&gt; &lt;beans:bean id="StubUser" class="com.somePackageName.StubUser"/&gt; &lt;beans:bean id="StubUserFactory" class="com.somePackageName.StubUserFactory"/&gt; </code></pre> <p>The error that I"m getting is:</p> <pre><code>java.lang.NullPointerException at com.somePackageName.security.UserDetailsImpl.&lt;init&gt;(UserDetailsImpl.java:44) at com.somePackageName.security.QuadstoreUserService.loadUserByUsername(QuadstoreUserService.java:23) at com.somePackageName.SpringSecurityTest.firstTest(SpringSecurityTest.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) </code></pre> <p>I am really stuck on this one, as I can't seem to find a good and clear example on the internet. I very much thank you in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload