forked from snychka/java-first-program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppTest.java
More file actions
56 lines (44 loc) · 2.08 KB
/
Copy pathAppTest.java
File metadata and controls
56 lines (44 loc) · 2.08 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
package com.h2;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.function.Try;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.platform.commons.util.ReflectionUtils.*;
public class AppTest {
private final String classToFind = "com.h2.App";
public Optional<Class<?>> getAppClass() {
Try<Class<?>> aClass = tryToLoadClass(classToFind);
return aClass.toOptional();
}
@Test
public void assertClassExistence() {
final Optional<Class<?>> maybeClass = getAppClass();
assertTrue(maybeClass.isPresent(), classToFind + " should be present");
assertEquals(classToFind, maybeClass.get().getCanonicalName());
}
@Test
public void assertPrivateMethodExistence() {
final String methodName = "add";
final Optional<Class<?>> maybeClass = getAppClass();
Class<?> aClass = maybeClass.get();
Optional<Method> maybeMethod = findMethod(aClass, methodName, int[].class);
assertTrue(maybeMethod.isPresent(), methodName + " should be present in " + aClass.getCanonicalName());
final Method method = maybeMethod.get();
assertTrue(isPrivate(method), methodName + " should be private");
assertEquals(int.class, method.getReturnType(), methodName + " should return type should be 'int'");
Parameter[] parameters = method.getParameters();
assertEquals(1, parameters.length, methodName + " should have 1 parameter");
assertEquals(int[].class, parameters[0].getType(), methodName + " parameter should be of type 'int[]'");
assertTrue(isStatic(method), methodName + "should be static method");
assertTrue(isPrivate(method), methodName + "should be private method");
}
@Test
public void testDoubleTheNumber() {
for (int i = 1; i < 10; i++) {
assertEquals(2 * i, App.doubleTheNumber(i), i + " should be " + 2 * i);
}
}
}