Skip to content

Instantly share code, notes, and snippets.

@nick45chen
Last active January 22, 2019 03:44
Show Gist options
  • Save nick45chen/fe3a06c0ddf06d97df530846da3cd570 to your computer and use it in GitHub Desktop.
Save nick45chen/fe3a06c0ddf06d97df530846da3cd570 to your computer and use it in GitHub Desktop.
Mockito failed
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.circlecisample"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.+'
}
package com.example.circlecisample;
import java.util.Calendar;
public class Holiday {
private static final int DECEMBER = 12;
private static final int DAY_OF_XMAS = 25;
private static final int DAY_OF_XMAS_NIGHT = 24;
public String sayXmas() {
int month = getCurrentMonth();
int day = getCurrentDayOfMonth();
if (month == DECEMBER && (day == DAY_OF_XMAS || day == DAY_OF_XMAS_NIGHT)) {
return "Merry Xmas";
} else {
return "Today is not Xmas";
}
}
protected int getCurrentMonth() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.MONTH) + 1;
}
protected int getCurrentDayOfMonth() {
Calendar calendar = Calendar.getInstance();
return calendar.get(Calendar.DAY_OF_MONTH);
}
}
package com.example.circlecisample;
import org.junit.Test;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
public class HolidayTest {
@Test
public void today_is_xmas_12_25() {
Holiday holiday = mock(Holiday.class);
when(holiday.getCurrentMonth()).thenReturn(12);
when(holiday.getCurrentDayOfMonth()).thenReturn(25);
assertEquals("Merry Xmas", holiday.sayXmas());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment