robolectric : robolectric의 설정법은 생략한다. 공식 홈 페이지를 참고하도록
참고 url: http://www.vogella.com/tutorials/Robolectric/article.html
예전에 작성해 둔 EventBus에 대한 test code를 작성해 보았다.
package com.mdiwebma.base.task;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import com.mdiwebma.base.BuildConfig;
import com.mdiwebma.base.annotation.Subscribe;
import com.mdiwebma.base.settings.SettingChangedEvent;
import com.mdiwebma.base.settings.Settings;
/**
* @author djkim
*/
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, manifest = "test/AndroidManifest.xml", sdk = 21, resourceDir = "res")
public class EventBusTest {
@Before
public void onBefore() {
eventHistory.clear();
EventBus.getDefault().register(this);
}
@After
public void onAfter() {
EventBus.getDefault().unregister(this);
}
@Test
public void testEventBus() {
// test unregister
EventBus.getDefault().unregister(this);
EventBus.getDefault().post(new SettingChangedEvent(Settings.test_int));
assertThat(eventHistory.size()).isEqualTo(0);
EventBus.getDefault().register(this);
EventBus.getDefault().post(new SettingChangedEvent(Settings.test_int));
assertThat(eventHistory.size()).isEqualTo(1);
//test duplicate register
EventBus.getDefault().register(this);
EventBus.getDefault().post(new SettingChangedEvent(Settings.test_int));
assertThat(eventHistory.size()).isEqualTo(2);
assertThat(eventHistory.get(eventHistory.size() - 1).setting).isEqualTo(Settings.test_int);
EventBus.getDefault().post(new SettingChangedEvent(Settings.test_string));
assertThat(eventHistory.get(eventHistory.size() - 1).setting).isEqualTo(Settings.test_string);
EventBus.getDefault().post(new SettingChangedEvent(Settings.test_bool));
assertThat(eventHistory.get(eventHistory.size() - 1).setting).isEqualTo(Settings.test_bool);
EventBus.getDefault().post(new SettingChangedEvent(Settings.test_int));
assertThat(eventHistory.size()).isEqualTo(5);
// test duplicate unregister
EventBus.getDefault().unregister(this);
}
List<SettingChangedEvent> eventHistory = new ArrayList<>();
@Subscribe
public void onEvent(SettingChangedEvent event) {
eventHistory.add(event);
}
}
상단 소스 탭에서 "EventBusTest"에서 우클릭하여 "Run EventBustTest with Coverage"를 실행한다. 테스트 코드가 성공하면 report를 html로 export할 수 있는데, html 파일을 열어보면 테스트가 된 코드(녹색)가 안 된 코드를(붉은색) 색깔로 확인할 수 있다.
SettingsDaoTest의 결과인데, SettingsDao은 Boolean과 Integer, String 타입에서만 구현이 되었기 때문에 그 와 타입에 대해서는 테스트되지 않아 붉은색으로 표시되고 있다.
1 package com.mdiwebma.base.settings; 2 3 import android.support.annotation.NonNull; 4 5 import com.mdiwebma.base.helper.StringBuilderPool; 6 7 /** 8 * @author djkim 9 */ 10 11 public enum Settings { 12 13 test_bool(Settings.BooleanType, Boolean.TRUE), 14 test_string(Settings.StringType, "test_str"), 15 test_int(Settings.IntegerType, (Integer)100); 16 17 public static final int BooleanType = 0; 18 public static final int StringType = 1; 19 public static final int IntegerType = 2; 20 public static final int LongType = 3; 21 public static final int FloatType = 4; 22 public static final int DoubleType = 5; 23 24 public final int settingType; 25 public final Object defaultValue; 26 public int cacheIndex; // do not modify manually, initialized from SettingsDao static scope 27 28 private Settings(int settingType, @NonNull Object defaultValue) { 29 this.settingType = settingType; 30 this.defaultValue = defaultValue; 31 32 switch (this.settingType) { 33 case BooleanType: 34 if ((defaultValue instanceof Boolean) == false) { 35 throw new AssertionError(StringBuilderPool.concat("type mismatch=", this.name())); 36 } 37 break; 38 case StringType: 39 if ((defaultValue instanceof String) == false) { 40 throw new AssertionError(StringBuilderPool.concat("type mismatch=", this.name())); 41 } 42 break; 43 case IntegerType: 44 if ((defaultValue instanceof Integer) == false) { 45 throw new AssertionError(StringBuilderPool.concat("type mismatch=", this.name())); 46 } 47 break; 48 case LongType: 49 if ((defaultValue instanceof Long) == false) { 50 throw new AssertionError(StringBuilderPool.concat("type mismatch=", this.name())); 51 } 52 break; 53 case FloatType: 54 if ((defaultValue instanceof Float) == false) { 55 throw new AssertionError(StringBuilderPool.concat("type mismatch=", this.name())); 56 } 57 break; 58 case DoubleType: 59 if ((defaultValue instanceof Double) == false) { 60 throw new AssertionError(StringBuilderPool.concat("type mismatch=", this.name())); 61 } 62 break; 63 default: 64 throw new AssertionError(StringBuilderPool.concat("unsupported key type=", this.name())); 65 } 66 } 67 68
}