1+ package com .blogspot .e_kanivets .moneytracker .repo ;
2+
3+ import android .content .ContentValues ;
4+ import android .database .Cursor ;
5+
6+ import com .blogspot .e_kanivets .moneytracker .DbHelper ;
7+ import com .blogspot .e_kanivets .moneytracker .entity .Transfer ;
8+
9+ import junit .framework .TestCase ;
10+
11+ import org .mockito .Mockito ;
12+
13+ import java .util .ArrayList ;
14+ import java .util .List ;
15+
16+ /**
17+ * Android Test case.
18+ * Created on 3/1/16.
19+ *
20+ * @author Evgenii Kanivets
21+ */
22+ public class TransferRepoTest extends TestCase {
23+ private TransferRepo repo ;
24+
25+ @ Override
26+ public void setUp () throws Exception {
27+ super .setUp ();
28+ DbHelper mock = Mockito .mock (DbHelper .class );
29+ repo = new TransferRepo (mock );
30+ }
31+
32+ @ Override
33+ public void tearDown () throws Exception {
34+ super .tearDown ();
35+ repo = null ;
36+ }
37+
38+ public void testGetTable () throws Exception {
39+ assertEquals (DbHelper .TABLE_TRANSFERS , repo .getTable ());
40+ }
41+
42+ public void testContentValues () throws Exception {
43+ Transfer transfer = new Transfer (1 , 1 , 1 , 2 , 100 , 200 );
44+
45+ ContentValues expected = new ContentValues ();
46+ expected .put (DbHelper .TIME_COLUMN , 1L );
47+ expected .put (DbHelper .FROM_ACCOUNT_ID_COLUMN , 1L );
48+ expected .put (DbHelper .TO_ACCOUNT_ID_COLUMN , 2L );
49+ expected .put (DbHelper .FROM_AMOUNT_COLUMN , 100 );
50+ expected .put (DbHelper .TO_AMOUNT_COLUMN , 200 );
51+
52+ ContentValues actual = repo .contentValues (transfer );
53+
54+ assertEquals (expected , actual );
55+
56+ assertNull (repo .contentValues (null ));
57+ }
58+
59+ public void testGetListFromCursor () throws Exception {
60+ assertEquals (new ArrayList <Transfer >(), repo .getListFromCursor (Mockito .mock (Cursor .class )));
61+
62+ Cursor mockCursor = Mockito .mock (Cursor .class );
63+ Mockito .when (mockCursor .moveToFirst ()).thenReturn (true );
64+ Mockito .when (mockCursor .getColumnIndex (DbHelper .ID_COLUMN )).thenReturn (1 );
65+ Mockito .when (mockCursor .getColumnIndex (DbHelper .TIME_COLUMN )).thenReturn (2 );
66+ Mockito .when (mockCursor .getColumnIndex (DbHelper .FROM_ACCOUNT_ID_COLUMN )).thenReturn (3 );
67+ Mockito .when (mockCursor .getColumnIndex (DbHelper .TO_ACCOUNT_ID_COLUMN )).thenReturn (4 );
68+ Mockito .when (mockCursor .getColumnIndex (DbHelper .FROM_AMOUNT_COLUMN )).thenReturn (5 );
69+ Mockito .when (mockCursor .getColumnIndex (DbHelper .TO_AMOUNT_COLUMN )).thenReturn (6 );
70+
71+ Mockito .when (mockCursor .getLong (1 )).thenReturn (1L );
72+ Mockito .when (mockCursor .getLong (2 )).thenReturn (1L );
73+ Mockito .when (mockCursor .getLong (3 )).thenReturn (1L );
74+ Mockito .when (mockCursor .getLong (4 )).thenReturn (2L );
75+ Mockito .when (mockCursor .getInt (5 )).thenReturn (100 );
76+ Mockito .when (mockCursor .getInt (6 )).thenReturn (200 );
77+
78+ List <Transfer > expected = new ArrayList <>();
79+ expected .add (new Transfer (1 , 1 , 1 , 2 , 100 , 200 ));
80+
81+ assertEquals (expected , repo .getListFromCursor (mockCursor ));
82+
83+ assertEquals (new ArrayList <>(), repo .getListFromCursor (null ));
84+ }
85+ }
0 commit comments