generated from ohjelmointi2/gradle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductTest.java
More file actions
65 lines (52 loc) · 2.26 KB
/
ProductTest.java
File metadata and controls
65 lines (52 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package inheritance.webshop;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.ReflectionUtils;
public class ProductTest {
// The object under test
private final Product book = new Product("Book", "Full of exciting stories", 100.0);
@Test
void getTitleIsPresentAndReturnsTheTitle() throws Exception {
checkMethod(book, "getTitle", "Book");
}
@Test
void getDescriptionIsPresentAndReturnsTheDescription() throws Exception {
checkMethod(book, "getDescription", "Full of exciting stories");
}
@Test
void getPriceIsPresentAndReturnsTheDescription() throws Exception {
checkMethod(book, "getPrice", 100.0);
}
@Test
void toStringContainsAllFields() {
String str = book.toString();
assertTrue(str.contains("Book"));
assertTrue(str.contains("Full of exciting stories"));
assertTrue(str.contains("100.0"));
}
/**
* This method checks that the given object has a method with the given
* name, and that the method returns the given value.
*
* This approach is not typically needed in unit tests, as you almost never
* need to check that a method exists in your own code. However, as the task
* is to create a class with specific methods, we need some (hacky) way to
* check that the method was created after this test was written, and that
* it returns the correct value.
*/
protected static void checkMethod(Object object, String methodName, Object expectedValue) {
Optional<Method> method = ReflectionUtils.findMethod(object.getClass(), methodName);
assertTrue(method.isPresent(),
"The class " + object.getClass() + " does not have a " + methodName + "() method");
try {
assertEquals(expectedValue, method.get().invoke(object));
} catch (IllegalAccessException | InvocationTargetException e) {
// This could happen for example if the method is not public
throw new RuntimeException(e);
}
}
}