Skip to content

Commit

Permalink
tests with robolectric
Browse files Browse the repository at this point in the history
  • Loading branch information
pblachut committed Apr 27, 2016
1 parent 61bc10c commit 227f928
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion LoginTest/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
testCompile "org.mockito:mockito-core:1.+"
testCompile 'org.mockito:mockito-core:1.+'
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile 'org.robolectric:robolectric:3.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package piotrek.logintest;

import android.content.SharedPreferences;

/**
* Created by Admin on 2016-04-27.
*/
public class CredentialsStorage implements ICredentialsStorage {
private final SharedPreferences preferences;

public CredentialsStorage(SharedPreferences preferences) {

this.preferences = preferences;
}

@Override
public void storeUsername(String username) {
preferences
.edit()
.putString("user", username)
.apply();

// apply - asynchroniczny
// commit - synchroniczny
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package piotrek.logintest;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPreferenceManager;

import static org.junit.Assert.*;

/**
* Created by Admin on 2016-04-27.
*/
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class ICredentialsStorageTest {

@Test
public void should_save_in_shared_preferences() throws Exception {
// prepare
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application);

ICredentialsStorage credentialsStorage = new CredentialsStorage(preferences);

// run
credentialsStorage.storeUsername("username");
// assert

String current = preferences.getString("user", null);
assertEquals("username", current);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ public void setUp() throws Exception {
when(api.login(anyString(), anyString()))
.thenReturn("OK");

sut = new LoginManager(credentialsStorage, api, view);
sut = new LoginManager(credentialsStorage, api);
}
}

0 comments on commit 227f928

Please sign in to comment.