Skip to content

Instantly share code, notes, and snippets.

@MaksimDmitriev
Created January 26, 2018 09:26
Show Gist options
  • Select an option

  • Save MaksimDmitriev/1ceea5c08abb5fa9dc485fce7f71f630 to your computer and use it in GitHub Desktop.

Select an option

Save MaksimDmitriev/1ceea5c08abb5fa9dc485fce7f71f630 to your computer and use it in GitHub Desktop.
PowerMock with System.currentTimeMillis
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "sample.com.sample_app"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
testImplementation 'org.mockito:mockito-core:2.8.9'
String powerMockVersion = "1.7.3"
testImplementation "org.powermock:powermock-module-junit4:${powerMockVersion}"
testImplementation "org.powermock:powermock-api-mockito2:${powerMockVersion}"
}
package sample.com.sample_app;
import android.support.annotation.NonNull;
public class FileUtils {
@NonNull
public static String generateName() {
return Long.toString(System.currentTimeMillis());
}
}
package sample.com.sample_app;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static junit.framework.TestCase.assertEquals;
@RunWith(PowerMockRunner.class)
@PrepareForTest({System.class})
public class FileUtilsTest {
@Test
public void generateName() {
PowerMockito.spy(System.class);
PowerMockito.when(System.currentTimeMillis()).thenReturn(100L);
String name = FileUtils.generateName();
assertEquals("100", name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment