Java 接口模拟gevis空指针异常

Java 接口模拟gevis空指针异常,java,junit,Java,Junit,我对单元测试有点陌生,但我尝试模拟接口。它在设置中提供一个空指针。有人能帮我模仿这些东西吗 代码如下: public class ClassToTest { @Mock private RequestContainer request; @Mock URL serviceEndPoint; @Mock HttpURLConnection connection; @Before

我对单元测试有点陌生,但我尝试模拟接口。它在设置中提供一个空指针。有人能帮我模仿这些东西吗

代码如下:

public class ClassToTest
        {

            @Mock private RequestContainer request;
            @Mock URL serviceEndPoint;
            @Mock HttpURLConnection connection;

            @Before
            public void setup() {
                try
                {  
                    //MockitoAnnotations.initMocks(this); // tried this, but failes because mockito can't mock url.
                    when(request.getServiceEndpoint()).thenReturn(serviceEndPoint); //nullpointer here
                }
                catch(MalformedURLException e)
                {
                    // setup failed!
                    assertTrue("setup failed, unable to create mock for service endpoint....", false);
                }    
            }
    }

//尝试了此操作,但失败,因为mockito无法模拟url。

所以,不要让mockito模拟URL。Mockito无法模拟
final

  • URL serviceEndPoint
    字段中删除
    @Mock
  • 恢复
    MockitoAnnotations.initMocks(此)
  • 紧接着,写
    serviceEndPoint=newURL(“http://example.com");
example.com
域是(通过RFC),因此如果您不小心没有模拟
HttpURLConnection
,您就永远不会与真正的服务器交谈

//尝试了此操作,但失败,因为mockito无法模拟url。

所以,不要让mockito模拟URL。Mockito无法模拟
final

  • URL serviceEndPoint
    字段中删除
    @Mock
  • 恢复
    MockitoAnnotations.initMocks(此)
  • 紧接着,写
    serviceEndPoint=newURL(“http://example.com");

example.com
域是(通过RFC),因此如果您不小心没有模拟
HttpURLConnection

,您是否考虑过从
URL serviceEndPoint
中删除
@mock
,然后手动实例化它?是的,但是稍后我想打开一个到端点的连接(从这个url)。因为我不想实际打开连接,所以我想模拟URL对象。。。在其中一个测试中,我使用了以下方法:when(serviceEndPoint.openConnection()).thenReturn(connection);
连接
不会“实际”打开-它是一个模拟。我希望它是一个模拟,但mockito不能这样做。。。显然…Mockito无法模拟
URL
,因为它是
最终的
;它可以模拟
HttpURLConnection
,因为它不是
final
。您是否考虑过从
URL serviceEndPoint
中删除
@mock
,然后手动实例化它?是的,但稍后我想打开到端点的连接(从此URL)。因为我不想实际打开连接,所以我想模拟URL对象。。。在其中一个测试中,我使用了以下方法:when(serviceEndPoint.openConnection()).thenReturn(connection);
连接
不会“实际”打开-它是一个模拟。我希望它是一个模拟,但mockito不能这样做。。。显然…Mockito无法模拟
URL
,因为它是
最终的
;它可以模拟
HttpURLConnection
,因为它不是
final
。但是当serviceEndPoint是有效的url实例时。。。如何模拟:何时(serviceEndPoint.openConnection())。然后返回(connection);它现在不起作用,因为serviceEndPoint不再是模拟的,您可能需要使用.Hmmm。。。。我真的不想。。。ClassToTest是正式创建实际连接并执行请求的类,因此我想让这个类创建连接,但我可以将接口从public URL getServiceEndpoint()更改为public HttpURLConnection getServiceConnection()。我想这会让测试更容易?谢谢,这让我更进一步。但是当serviceEndPoint是有效的url实例时。。。如何模拟:何时(serviceEndPoint.openConnection())。然后返回(connection);它现在不起作用,因为serviceEndPoint不再是模拟的,您可能需要使用.Hmmm。。。。我真的不想。。。ClassToTest是正式创建实际连接并执行请求的类,因此我想让这个类创建连接,但我可以将接口从public URL getServiceEndpoint()更改为public HttpURLConnection getServiceConnection()。我想这会让测试更容易?
 public interface RequestContainer
            {
                public String getJson() throws JsonProcessingException;
                public String getRequestMethod();
                public String getWebsiteKey();
                public String getSecretKey();
                public URL getServiceEndpoint() throws MalformedURLException;
            }