Skip to content

Commit

Permalink
mockito now
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleMeyers committed Oct 24, 2016
1 parent 5228842 commit ef77fd8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
3 changes: 3 additions & 0 deletions heroku/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ dependencies {
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//used for junit tests
testCompile "org.robolectric:robolectric:3.1.2"

//mockito for mocking the http connection for the tests
testCompile 'org.mockito:mockito-core:1.10.19'
}
82 changes: 66 additions & 16 deletions heroku/src/test/java/kylemeyers22/heroku/TeamUnitTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

package kylemeyers22.heroku;

import android.app.Activity;
import android.os.Bundle;

import org.apache.http.HttpStatus;
import org.junit.Before;

Expand All @@ -25,34 +28,81 @@


import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import retrofit2.Response;

/**
* Created by shdw2 on 10/23/2016.
*/

import org.mockito.Mockito;
import static org.mockito.Mockito.verify;
import static org.robolectric.Shadows.shadowOf;

import java.io.IOException;
import java.net.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import android.app.Activity;
import android.os.Bundle;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 23)
//@Config(constants = BuildConfig.class)

public class TeamUnitTest{

@Test
public void testTeamName()
{
try {
String jsonMimeType = "application/json";
HttpUriRequest request = new HttpGet("https://baseballsim.herokuapp.com/api/users/token");
@Mock
HttpURLConnection mockHttpConnection;

// When
HttpResponse response = HttpClientBuilder.create().build().execute(request);
HttpUrlActivity activity;

public class HttpUrlActivity extends Activity {

private HttpURLConnection httpConnection;

// this would be in the for real application
public HttpUrlActivity() throws MalformedURLException, IOException {
this((HttpURLConnection) new URL("http://baseballsim.heroku.com/api/users").openConnection());
}

// this is how we inject the mock in our test
public HttpUrlActivity(final HttpURLConnection httpConnection) {
this.httpConnection = httpConnection;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
try {
httpConnection.setRequestMethod("GET");
} catch (ProtocolException e) {
throw new RuntimeException(e);
}
super.onCreate(savedInstanceState);
}

// Then
assertThat(response.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_NOT_FOUND));
// String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
//assertEquals(jsonMimeType, mimeType);
}
catch(IOException e)
{
e.printStackTrace();
}

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
activity = new HttpUrlActivity(mockHttpConnection);
}

@Test
public void itCanTestHttpURLConnectionStuff() throws ProtocolException {
shadowOf(activity).recreate();
verify(mockHttpConnection).setRequestMethod("GET");
}
}

0 comments on commit ef77fd8

Please sign in to comment.