Skip to content

Commit 21322c7

Browse files
committed
Usage of Hamcrest
1 parent fe7db42 commit 21322c7

3 files changed

Lines changed: 66 additions & 11 deletions

File tree

‎com.vogella.android.temperatureconverter/app/build.gradle‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,23 @@ apply plugin: 'com.android.application'
33

44
android {
55
compileSdkVersion 23
6-
buildToolsVersion "23.0.0"
6+
buildToolsVersion "22.0.1"
7+
78
defaultConfig {
89
applicationId "com.vogella.android.temperatureconverter"
910
minSdkVersion 21
1011
targetSdkVersion 21
1112
versionCode 1
1213
versionName "1.0"
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1315
}
14-
buildTypes {
15-
release {
16-
minifyEnabled false
17-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18-
}
19-
}
20-
productFlavors {
21-
}
22-
}
2316

17+
}
2418

2519
dependencies {
26-
compile fileTree(include: ['*.jar'], dir: 'libs')
2720
testCompile 'junit:junit:4.12'
21+
testCompile 'org.hamcrest:hamcrest-library:1.3'
22+
androidTestCompile 'junit:junit:4.12'
23+
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
24+
androidTestCompile 'com.android.support.test:runner:0.3'
2825
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.vogella.android.temperatureconverter;
2+
3+
import android.support.test.runner.AndroidJUnit4;
4+
5+
import static org.junit.Assert.*;
6+
7+
import org.junit.Test;
8+
import org.junit.*;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.assertTrue;
12+
13+
@RunWith(AndroidJUnit4.class)
14+
public class ConverterUtilTest2 {
15+
16+
17+
@Test
18+
public void testConvertFahrenheitToCelsius() {
19+
float actual = ConverterUtil.convertCelsiusToFahrenheit(100);
20+
// expected value is 212
21+
float expected = 212;
22+
// use this method because float is not precise
23+
assertEquals("Conversion from celsius to fahrenheit failed", expected, actual, 0.001);
24+
}
25+
26+
@Test
27+
public void testConvertCelsiusToFahrenheit() {
28+
float actual = ConverterUtil.convertFahrenheitToCelsius(212);
29+
// expected value is 100
30+
float expected = 100;
31+
// use this method because float is not precise
32+
assertEquals("Conversion from celsius to fahrenheit failed", expected, actual, 0.001);
33+
}
34+
}

‎com.vogella.android.temperatureconverter/app/src/test/java/com/vogella/android/temperatureconverter/ConverterUtilTest.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import org.junit.Test;
66
import org.junit.*;
77

8+
import java.util.Arrays;
9+
import java.util.List;
10+
811
import static org.junit.Assert.assertTrue;
12+
import static org.hamcrest.Matchers.*;
913

1014
public class ConverterUtilTest {
1115

@@ -19,6 +23,26 @@ public void testConvertFahrenheitToCelsius() {
1923
assertEquals("Conversion from celsius to fahrenheit failed", expected, actual, 0.001);
2024
}
2125

26+
@Test
27+
public void testConvertFahrenheitToCelsiusHamcrest() {
28+
float actual = ConverterUtil.convertCelsiusToFahrenheit(100);
29+
// expected value is 212
30+
float expected = 212;
31+
32+
// Hamcrest
33+
assertThat("Conversion from celsius to fahrenheit failed", expected, is(actual));
34+
}
35+
36+
// Hamcrest
37+
@Test
38+
public void checkListContent() {
39+
List<Integer> list = Arrays.asList(5, 2, 4);
40+
assertThat(list, hasSize(3));
41+
assertThat(list, containsInAnyOrder(5, 2, 4));
42+
assertThat(list, containsInAnyOrder(2, 4, 5));
43+
assertThat(list, everyItem(greaterThan(1)));
44+
}
45+
2246
@Test
2347
public void testConvertCelsiusToFahrenheit() {
2448
float actual = ConverterUtil.convertFahrenheitToCelsius(212);

0 commit comments

Comments
 (0)