Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit d530379

Browse files
author
Evgenii Kanivets
committed
#23[30m]. Add unit test for ExchangeRateProvider.
1 parent 5de959c commit d530379

File tree

4 files changed

+135
-1
lines changed

4 files changed

+135
-1
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ dependencies {
5454
compile 'com.google.dagger:dagger:2.0.1'
5555
apt 'com.google.dagger:dagger-compiler:2.0.1'
5656
provided 'org.glassfish:javax.annotation:10.0-b28'
57+
58+
testCompile 'junit:junit:4.12'
5759
}

app/src/main/java/com/blogspot/e_kanivets/moneytracker/model/ExchangeRate.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public double getAmount() {
5050
return amount;
5151
}
5252

53+
@Override
54+
public boolean equals(Object o) {
55+
if (o instanceof ExchangeRate) {
56+
ExchangeRate rate = (ExchangeRate) o;
57+
return this.id == rate.getId()
58+
&& this.createdAt == rate.getCreatedAt()
59+
&& this.fromCurrency.equals(rate.getFromCurrency())
60+
&& this.toCurrency.equals(rate.getToCurrency())
61+
&& this.amount == rate.getAmount();
62+
} else return false;
63+
}
64+
5365
@SuppressWarnings("StringBufferReplaceableByString")
5466
@Override
5567
public String toString() {
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.blogspot.e_kanivets.moneytracker.report;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import com.blogspot.e_kanivets.moneytracker.controller.ExchangeRateController;
7+
import com.blogspot.e_kanivets.moneytracker.model.ExchangeRate;
8+
import com.blogspot.e_kanivets.moneytracker.model.Record;
9+
import com.blogspot.e_kanivets.moneytracker.repo.base.IRepo;
10+
import com.blogspot.e_kanivets.moneytracker.report.base.IExchangeRateProvider;
11+
12+
import org.junit.After;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
import static org.junit.Assert.*;
20+
21+
/**
22+
* JUnit4 test case.
23+
* Created on 2/25/16.
24+
*
25+
* @author Evgenii Kanivets
26+
*/
27+
public class ExchangeRateProviderTest {
28+
private ExchangeRateController rateController;
29+
30+
@Before
31+
public void setUp() throws Exception {
32+
rateController = new ExchangeRateController(new TestRepo());
33+
}
34+
35+
@After
36+
public void tearDown() throws Exception {
37+
rateController = null;
38+
}
39+
40+
@Test
41+
public void testGetRate() throws Exception {
42+
IExchangeRateProvider provider;
43+
44+
try {
45+
provider = new ExchangeRateProvider(null, rateController);
46+
} catch (NullPointerException e) {
47+
provider = null;
48+
}
49+
50+
assertNull(provider);
51+
52+
try {
53+
provider = new ExchangeRateProvider("", null);
54+
} catch (NullPointerException e) {
55+
provider = null;
56+
}
57+
58+
assertNull(provider);
59+
60+
try {
61+
provider = new ExchangeRateProvider(null, null);
62+
} catch (NullPointerException e) {
63+
provider = null;
64+
}
65+
66+
assertNull(provider);
67+
68+
provider = new ExchangeRateProvider("USD", rateController);
69+
70+
assertEquals(new ExchangeRate(1, "UAH", "USD", 4),
71+
provider.getRate(new Record(0, 0, "", "", 0, 0, "UAH")));
72+
73+
assertEquals(new ExchangeRate(0, "AFN", "USD", 3),
74+
provider.getRate(new Record(0, 0, "", "", 0, 0, "AFN")));
75+
76+
assertNull(provider.getRate(new Record(0, 0, "", "", 0, 0, "SMTH")));
77+
}
78+
79+
private static class TestRepo implements IRepo<ExchangeRate> {
80+
@Nullable
81+
@Override
82+
public ExchangeRate create(ExchangeRate instance) {
83+
return null;
84+
}
85+
86+
@Nullable
87+
@Override
88+
public ExchangeRate read(long id) {
89+
return null;
90+
}
91+
92+
@Nullable
93+
@Override
94+
public ExchangeRate update(ExchangeRate instance) {
95+
return null;
96+
}
97+
98+
@Override
99+
public boolean delete(ExchangeRate instance) {
100+
return false;
101+
}
102+
103+
@NonNull
104+
@Override
105+
public List<ExchangeRate> readAll() {
106+
List<ExchangeRate> rateList = new ArrayList<>();
107+
rateList.add(new ExchangeRate(1, "UAH", "USD", 4));
108+
rateList.add(new ExchangeRate(0, "UAH", "USD", 2));
109+
rateList.add(new ExchangeRate(0, "AFN", "USD", 3));
110+
rateList.add(new ExchangeRate(0, "USD", "EUR", 20));
111+
return rateList;
112+
}
113+
114+
@NonNull
115+
@Override
116+
public List<ExchangeRate> readWithCondition(String condition, String[] args) {
117+
return new ArrayList<>();
118+
}
119+
}
120+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ buildscript {
33
jcenter()
44
}
55
dependencies {
6-
classpath 'com.android.tools.build:gradle:1.3.0'
6+
classpath 'com.android.tools.build:gradle:1.5.0'
77
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
88
}
99
}

0 commit comments

Comments
 (0)