简体   繁体   English

Java @Inject无法正常工作-方法返回null

[英]Java @Inject not working - method returns null

Not sure as to why the interface doesn't get injected. 不确定为什么没有注入接口。 The test always returns null. 测试始终返回null。 I've got the beans.xml as well in WEB-INF . 我在WEB-INF也有beans.xml Why does it return null? 为什么返回null?

I've also tried to annotate the service class with @ApplicationScoped and a class that produces a new TImpl 我还尝试使用@ApplicationScoped和一个生成新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. 您说您的测试返回null ,但是stacktrace讲述了一个不同的故事: expected:<6> but was:<0>不是NullPointerException ,这意味着对service.test_method_service(3)的调用返回0 –为零。

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. 这似乎就像Mockito模拟的预期行为:将T的模拟注入到service实例中,在T的模拟中调用test_method() ,并且Mockito的返回int方法的默认值为0。

When you want a different behaviour, you have to set the mock's behaviour: 当您想要其他行为时,必须设置模拟的行为:

Mockito.when(t.test_method(3)).thenReturn(6);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM