Not sure as to why the interface doesn't get injected. The test always returns null. I've got the beans.xml
as well in WEB-INF
. Why does it return null?
I've also tried to annotate the service class with @ApplicationScoped
and a class that produces a new TImpl
public interface T {
int test_method(int n );
public void addToSession(Session session);
}
@Handler // Qualifier
public TImpl implements T{
private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
public TImpl();
@Override
int test_method(int n){ return n * 2; }
@Override
public void addToSession(Session session){
sessions.add(session);
}
}
public class TService implements Serializable {
private @Inject @Handler T;
public TService() {}
...
int test_method_service(int n) { return T.test_method(n); }
public void addToSession(Session session) { T.addToSession(session); }
}
public class L extends Endpoint {
TService service;
public L(TService s){ this.service = t; }
public L(){}
@Override
public void OnOpen(Session session, ... )
servive.addToSession(session); // null pointer
...
}
Stacktrace
java.lang.NullPointerException
at com.vio.sockets.configuration.MessageEndPoint.onOpen(MessageEndPoint.java:40)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:914)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1457)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
You say that your test returns null
, but the stacktrace tells a different story: expected:<6> but was:<0>
is not a NullPointerException
, it means that the call to service.test_method_service(3)
return 0 – that's zero.
This seems just like the expected behaviour of a Mockito mock: You have a mock for T
injected into your service
instance, it calls test_method()
in the mock for T, and Mockito's default value for methods returning int
is 0.
When you want a different behaviour, you have to set the mock's behaviour:
Mockito.when(t.test_method(3)).thenReturn(6);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.