Skip to content

Commit dc17b86

Browse files
committed
Added real unit tests
1 parent 1be0582 commit dc17b86

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

com.vogella.android.temperatureconverter/app/src/test/java/com/vogella/android/temperatureconverter/AllTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@RunWith(Suite.class)
88
@SuiteClasses({
9-
ConverterTest.class
9+
ConverterUtilTest.class
1010
})
1111

1212
public class AllTests {

com.vogella.android.temperatureconverter/app/src/test/java/com/vogella/android/temperatureconverter/ConverterTest.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.vogella.android.temperatureconverter;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.*;
7+
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class ConverterUtilTest {
11+
12+
13+
@Test
14+
public void testConvertFahrenheitToCelsius() {
15+
float actual = ConverterUtil.convertCelsiusToFahrenheit(100);
16+
// expected value is 212
17+
float expected = 212;
18+
// use this method because float is not precise
19+
assertEquals("Conversion from celsius to fahrenheit failed", expected, actual, 0.001);
20+
}
21+
22+
@Test
23+
public void testConvertCelsiusToFahrenheit() {
24+
float actual = ConverterUtil.convertFahrenheitToCelsius(212);
25+
// expected value is 100
26+
float expected = 100;
27+
// use this method because float is not precise
28+
assertEquals("Conversion from celsius to fahrenheit failed", expected, actual, 0.001);
29+
}
30+
}

com.vogella.android.temperatureconverter/app/src/test/java/com/vogella/android/temperatureconverter/ParameterizedTestFields.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,25 @@ public class ParameterizedTestFields {
1616

1717
// fields used together with @Parameter must be public
1818
@Parameter
19-
public int m1;
19+
public float m1;
2020
@Parameter (value = 1)
21-
public int m2;
21+
public float m2;
2222

2323

2424
// creates the test data
2525
@Parameters
2626
public static Collection<Object[]> data() {
27-
Object[][] data = new Object[][] { { 1 , 2 }, { 5, 3 }, { 121, 4 } };
27+
Object[][] data = new Object[][] { { 46.4f , 8f }, { 5f, -15f }, { 24f, -4.44444465637207f } };
2828
return Arrays.asList(data);
2929
}
3030

3131

3232
@Test
3333
public void testMultiplyException() {
34-
MyClass tester = new MyClass();
35-
assertEquals("Result", m1 * m2, tester.multiply(m1, m2));
34+
float actual = ConverterUtil.convertFahrenheitToCelsius(m1);
35+
assertEquals("Result", m2, actual, 0.001);
3636
}
3737

3838

39-
// class to be tested
40-
class MyClass {
41-
public int multiply(int i, int j) {
42-
return i *j;
43-
}
44-
}
4539

4640
}

0 commit comments

Comments
 (0)