@Test public void testShouldCompareName() { Plane plane = new Plane("A380"); assertTrue(plane.sameAs("A380")); assertFalse(plane.sameAs("B747")); } Alt + Enter > Create method ‘sameAs’
@Test public void testShouldCompareName() { Plane plane = new Plane("A380"); assertTrue(plane.sameAs("A380")); assertFalse(plane.sameAs("B747")); } } OK
@Test public void testShouldCompareName() { Plane plane = new Plane("A380"); assertTrue(plane.sameAs("A380")); assertFalse(plane.sameAs("B747")); } } ! java.lang.RuntimeException: Method v in android.util.Log not mocked. at android.util.Log.v(Log.java) at com.skocken.junittest.Plane.sameAs(Plane.java:14) at com.skocken.junittest.PlaneTest.testShouldCompareName(PlaneTest.java:13)
@Test public void testShouldCompareName() { Plane plane = new Plane("A380"); assertTrue(plane.sameAs("A380")); assertFalse(plane.sameAs("B747")); } } OK
Solutions TextUtils → custom method (or library like Apache Common) Need to replace every calls More code “duplicate” “just because” of the test Tests clean, no change
BaseActivity:1600+ lines A base activity that handles common functionality in the app. This includes the navigation drawer, login and authentication, Action Bar tweaks, amongst others.
Architecture Presenter DataProvider ViewProxy Activity View Strong Reference Weak Reference Other “Strong” Reference on DataProvider, Presenter or ViewProxy forbidden. Use WeakReference. OK
@Override public void onResume() { super.onResume(); refreshBoard(); }
@Override public void onSelectBox(int x, int y) { getProvider().play(x, y); }
@Override public void refreshBoard() { GameBoard gameBoard = getProvider().getGameBoard(); int size = gameBoard.getSize(); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { setBoxValue(gameBoard, y, x); } } } v2: Presenter
@Override public void onResume() { super.onResume(); refreshBoard(); }
@Override public void onSelectBox(int x, int y) { getProvider().play(x, y); }
@Override public void refreshBoard() { GameBoard gameBoard = getProvider().getGameBoard(); int size = gameBoard.getSize(); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { setBoxValue(gameBoard, y, x); } } } v2: Presenter private void setBoxValue(GameBoard gameBoard, int y, int x) { GameBoard.Player player = gameBoard.getPlayer(x, y); int boxValue; switch (player) { case EMPTY: boxValue = BoardDef.BOX_EMPTY; break; case J1: boxValue = BoardDef.BOX_CROSS; break; case J2: boxValue = BoardDef.BOX_ROUND; break; default: return; } getView().setBoxValue(x, y, boxValue); } }
public class BoardViewProxy extends BaseViewProxy implements BoardDef.IView, View.OnClickListener {
public BoardViewProxy(Activity activity) { super(activity); }
@Override public void onClick(View v) { int x = (Integer) v.getTag(R.id.tag_x); int y = (Integer) v.getTag(R.id.tag_y); getPresenter().onSelectBox(x, y); } v2: ViewProxy @Override public void setBoxValue(int x, int y, int boxType) { ViewGroup boxLayout = findViewByIdEfficient(R.id.boxes_layout); ViewGroup rowLayout = (ViewGroup) boxLayout.getChildAt(y); View boxView = rowLayout.getChildAt(x);
int bgColor; switch (boxType) { case BoardDef.BOX_CROSS: bgColor = Color.RED; boxView.setOnClickListener(null); break; case BoardDef.BOX_ROUND: bgColor = Color.BLUE; boxView.setOnClickListener(null); break;
public class BoardViewProxy extends BaseViewProxy implements BoardDef.IView, View.OnClickListener {
public BoardViewProxy(Activity activity) { super(activity); }
@Override public void onClick(View v) { int x = (Integer) v.getTag(R.id.tag_x); int y = (Integer) v.getTag(R.id.tag_y); getPresenter().onSelectBox(x, y); } v2: ViewProxy @Override public void setBoxValue(int x, int y, int boxType) { ViewGroup boxLayout = findViewByIdEfficient(R.id.boxes_layout); ViewGroup rowLayout = (ViewGroup) boxLayout.getChildAt(y); View boxView = rowLayout.getChildAt(x);
Android Studio Execute app ↳ src ↳ main ↳ test ↳ java ↳ com.skocken.junittest ↳ ExampleUnitTest Right click > Run ‘All tests’ with Coverage (or Run ‘Junit’ with Coverage)