-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMyClassTest.java
More file actions
177 lines (150 loc) · 6.04 KB
/
Copy pathMyClassTest.java
File metadata and controls
177 lines (150 loc) · 6.04 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.*;
/**
* Test class for MyClass (exceptions demo)
*/
public class MyClassTest {
@Test(expected = MyRuntimeException.class)
public void testMainThrowsMyRuntimeExceptionForCase1() throws MyException {
MyClass.main(new String[]{});
}
@Test
public void testMyRuntimeExceptionIsCaught() throws MyException {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
try {
MyClass myClass = new MyClass();
// This should catch MyRuntimeException
} catch (MyRuntimeException e) {
// Expected
} finally {
System.setOut(originalOut);
}
}
@Test(expected = MyError.class)
public void testMethod1ThrowsMyErrorForCase2() throws Exception {
MyClass myClass = new MyClass();
java.lang.reflect.Method method = MyClass.class.getDeclaredMethod("method1", int.class);
method.setAccessible(true);
method.invoke(myClass, 2);
}
@Test(expected = MyException.class)
public void testMethod1ThrowsMyExceptionForCase3() throws Exception {
MyClass myClass = new MyClass();
java.lang.reflect.Method method = MyClass.class.getDeclaredMethod("method1", int.class);
method.setAccessible(true);
try {
method.invoke(myClass, 3);
} catch (java.lang.reflect.InvocationTargetException e) {
if (e.getCause() instanceof MyException) {
throw (MyException) e.getCause();
}
throw e;
}
}
@Test
public void testMyClassCreation() {
MyClass myClass = new MyClass();
assertNotNull(myClass);
}
@Test
public void testMyClassHasMainMethod() throws NoSuchMethodException {
assertNotNull(MyClass.class.getMethod("main", String[].class));
}
@Test
public void testMyClassHasMethod1() throws NoSuchMethodException {
assertNotNull(MyClass.class.getDeclaredMethod("method1", int.class));
}
@Test
public void testExceptionHandling() {
boolean runtimeExceptionCaught = false;
try {
throw new MyRuntimeException();
} catch (MyRuntimeException e) {
runtimeExceptionCaught = true;
}
assertTrue(runtimeExceptionCaught);
}
@Test
public void testMyRuntimeExceptionPrintsMessage() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
MyRuntimeException exception = new MyRuntimeException();
System.setOut(originalOut);
assertTrue(outContent.toString().contains("MyRuntimeException.MyRuntimeException"));
}
@Test
public void testMethod1ThrowsMyRuntimeExceptionForCase1() throws Exception {
MyClass myClass = new MyClass();
java.lang.reflect.Method method = MyClass.class.getDeclaredMethod("method1", int.class);
method.setAccessible(true);
try {
method.invoke(myClass, 1);
fail("Expected MyRuntimeException");
} catch (java.lang.reflect.InvocationTargetException e) {
assertTrue(e.getCause() instanceof MyRuntimeException);
}
}
@Test
public void testMethod1DefaultCaseNoException() throws Exception {
// Test that passing a value other than 1, 2, or 3 doesn't throw any exception
MyClass myClass = new MyClass();
java.lang.reflect.Method method = MyClass.class.getDeclaredMethod("method1", int.class);
method.setAccessible(true);
// Should not throw any exception for case 0
method.invoke(myClass, 0);
// Should not throw any exception for case 4
method.invoke(myClass, 4);
// Should not throw any exception for negative numbers
method.invoke(myClass, -1);
}
@Test
public void testMethod1AllCases() throws Exception {
MyClass myClass = new MyClass();
java.lang.reflect.Method method = MyClass.class.getDeclaredMethod("method1", int.class);
method.setAccessible(true);
// Test case 1 - MyRuntimeException
try {
method.invoke(myClass, 1);
fail("Expected MyRuntimeException for case 1");
} catch (java.lang.reflect.InvocationTargetException e) {
assertTrue("Case 1 should throw MyRuntimeException", e.getCause() instanceof MyRuntimeException);
}
// Test case 2 - MyError
try {
method.invoke(myClass, 2);
fail("Expected MyError for case 2");
} catch (java.lang.reflect.InvocationTargetException e) {
assertTrue("Case 2 should throw MyError", e.getCause() instanceof MyError);
}
// Test case 3 - MyException
try {
method.invoke(myClass, 3);
fail("Expected MyException for case 3");
} catch (java.lang.reflect.InvocationTargetException e) {
assertTrue("Case 3 should throw MyException", e.getCause() instanceof MyException);
}
}
@Test
public void testMethod1IsPrivate() throws NoSuchMethodException {
java.lang.reflect.Method method = MyClass.class.getDeclaredMethod("method1", int.class);
assertTrue(java.lang.reflect.Modifier.isPrivate(method.getModifiers()));
}
@Test
public void testMainMethodDeclaresMyException() throws NoSuchMethodException {
java.lang.reflect.Method mainMethod = MyClass.class.getMethod("main", String[].class);
Class<?>[] exceptionTypes = mainMethod.getExceptionTypes();
boolean declaresMyException = false;
for (Class<?> exceptionType : exceptionTypes) {
if (exceptionType == MyException.class) {
declaresMyException = true;
break;
}
}
assertTrue("main method should declare MyException", declaresMyException);
}
}