私はunittestingに少し新しいですが、私はインターフェイスを模擬しようとします。これは、セットアップでヌルポインタを与えます。誰も私がこれらのオブジェクトを嘲笑するのを助けることができる?ここでインターフェイスモックgevis nullpointer例外
コードです:だから
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);
}
}
}
public interface RequestContainer
{
public String getJson() throws JsonProcessingException;
public String getRequestMethod();
public String getWebsiteKey();
public String getSecretKey();
public URL getServiceEndpoint() throws MalformedURLException;
}
あなたは 'URL serviceEndPoint'から' @ Mock'を削除し、手でインスタンス化することを検討しましたか? –
はい、それ以降は(このURLからの)エンドポイントへの接続を開きたいとします。私は実際に接続を開けたくないので、私はURLオブジェクトを嘲笑したい...私はこれを使うテストの1つで:when(serviceEndPoint.openConnection())。then return(connection); – JohannisK
''接続 'は実際には開かれません - それはモックです。 –