Skip to content

Commit 9dcaf78

Browse files
author
Daniel da Silva
committed
Adding tests
1 parent e475d39 commit 9dcaf78

File tree

7 files changed

+81
-1
lines changed

7 files changed

+81
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Eclipse files
2+
.project
3+
.classpath
4+
15
# Build files
26
*.class
37
build

build.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
<project name="Ubidots">
22
<path id="Ubidots.classpath">
3+
<pathelement location="build/classes" />
4+
5+
<!-- the following jar's are used by the library -->
36
<pathelement location="libs/gson-2.2.4.jar" />
47
<pathelement location="libs/org.apache.httpcomponents.httpcore_4.3.jar" />
58
<pathelement location="libs/org.apache.httpcomponents.httpclient_4.3.jar" />
69
<pathelement location="libs/commons-logging-1.1.2.jar" />
10+
11+
<!-- The following jar's are used in tests -->
12+
<pathelement location="libs/mockito-all-1.9.5.jar" />
13+
<pathelement location="libs/junit-4.11.jar" />
14+
<pathelement location="libs/hamcrest-core-1.3.jar" />
715
</path>
816

917
<target name="clean">
@@ -21,4 +29,14 @@
2129
<mkdir dir="build/jar"/>
2230
<jar destfile="build/jar/ubidots.jar" basedir="build/classes" />
2331
</target>
24-
</project>
32+
33+
<target name="test">
34+
<junit fork="yes" printsummary="false">
35+
<classpath refid="Ubidots.classpath"/>
36+
<formatter type="brief" usefile="false"/>
37+
<batchtest filtertrace="true">
38+
<fileset dir="build/classes/" includes="**/*Test.class"/>
39+
</batchtest>
40+
</junit>
41+
</target>
42+
</project>

libs/hamcrest-core-1.3.jar

44 KB
Binary file not shown.

libs/junit-4.11.jar

239 KB
Binary file not shown.

libs/mockito-all-1.9.5.jar

1.51 MB
Binary file not shown.

src/com/ubidots/ApiClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,9 @@ public Variable[] getVariables() {
8585

8686
return variables;
8787
}
88+
89+
// For testing
90+
void setServerBridge(ServerBridge bridge) {
91+
this.bridge = bridge;
92+
}
8893
}

src/com/ubidots/ApiClientTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.ubidots;
2+
3+
import static org.junit.Assert.*;
4+
import org.junit.Test;
5+
import static org.mockito.Mockito.*;
6+
7+
public class ApiClientTest {
8+
9+
@Test
10+
public void testGetVariablesEmptyList() {
11+
ServerBridge bridge = mock(ServerBridge.class);
12+
when(bridge.get("variables")).thenReturn("[]");
13+
14+
ApiClient api = new ApiClient("abc");
15+
api.setServerBridge(bridge);
16+
Variable[] variables = api.getVariables();
17+
18+
assertEquals(variables.length, 0);
19+
}
20+
21+
@Test
22+
public void testGetVariablesSingleVar() {
23+
ServerBridge bridge = mock(ServerBridge.class);
24+
when(bridge.get("variables")).thenReturn(
25+
"[{'name': 'A', 'id': 'a', 'icon': 'temperature'}]"
26+
);
27+
28+
ApiClient api = new ApiClient("abc");
29+
api.setServerBridge(bridge);
30+
Variable[] variables = api.getVariables();
31+
32+
assertEquals(variables.length, 1);
33+
}
34+
35+
@Test
36+
public void testGetVariablesFiveVars() {
37+
ServerBridge bridge = mock(ServerBridge.class);
38+
when(bridge.get("variables")).thenReturn("[" +
39+
"{'name': 'A', 'id': 'a', 'icon': 'temperature'}," +
40+
"{'name': 'B', 'id': 'b', 'icon': 'temperature'}," +
41+
"{'name': 'C', 'id': 'c', 'icon': 'temperature'}," +
42+
"{'name': 'D', 'id': 'd', 'icon': 'temperature'}," +
43+
"{'name': 'E', 'id': 'e', 'icon': 'heart'}" +
44+
"]"
45+
);
46+
47+
ApiClient api = new ApiClient("abc");
48+
api.setServerBridge(bridge);
49+
Variable[] variables = api.getVariables();
50+
51+
assertEquals(variables.length, 5);
52+
}
53+
}

0 commit comments

Comments
 (0)