2020import org .junit .jupiter .api .Assertions ;
2121import org .junit .jupiter .api .Test ;
2222
23- import static org .junit .jupiter .api .Assertions .fail ;
24-
2523public class RpcResultTest {
2624 @ Test
27- public void testRecreateWithNormalException () {
25+ public void testRpcResultWithNormalException () {
2826 NullPointerException npe = new NullPointerException ();
2927 RpcResult rpcResult = new RpcResult (npe );
30- try {
31- rpcResult .recreate ();
32- fail ();
33- } catch (Throwable throwable ) {
34- StackTraceElement [] stackTrace = throwable .getStackTrace ();
35- Assertions .assertNotNull (stackTrace );
36- Assertions .assertTrue (stackTrace .length > 1 );
28+
29+ StackTraceElement [] stackTrace = rpcResult .getException ().getStackTrace ();
30+ Assertions .assertNotNull (stackTrace );
31+ Assertions .assertTrue (stackTrace .length > 1 );
32+ }
33+
34+ /**
35+ * please run this test in Run mode
36+ */
37+ @ Test
38+ public void testRpcResultWithEmptyStackTraceException () {
39+ Throwable throwable = buildEmptyStackTraceException ();
40+ if (throwable == null ) {
41+ return ;
3742 }
43+ RpcResult rpcResult = new RpcResult (throwable );
44+
45+ StackTraceElement [] stackTrace = rpcResult .getException ().getStackTrace ();
46+ Assertions .assertNotNull (stackTrace );
47+ Assertions .assertTrue (stackTrace .length == 0 );
48+ }
49+
50+ @ Test
51+ public void testSetExceptionWithNormalException () {
52+ NullPointerException npe = new NullPointerException ();
53+ RpcResult rpcResult = new RpcResult ();
54+ rpcResult .setException (npe );
55+
56+ StackTraceElement [] stackTrace = rpcResult .getException ().getStackTrace ();
57+ Assertions .assertNotNull (stackTrace );
58+ Assertions .assertTrue (stackTrace .length > 1 );
3859 }
3960
4061 /**
4162 * please run this test in Run mode
4263 */
4364 @ Test
44- public void testRecreateWithEmptyStackTraceException () {
65+ public void testSetExceptionWithEmptyStackTraceException () {
66+ Throwable throwable = buildEmptyStackTraceException ();
67+ if (throwable == null ) {
68+ return ;
69+ }
70+ RpcResult rpcResult = new RpcResult ();
71+ rpcResult .setException (throwable );
72+
73+ StackTraceElement [] stackTrace = rpcResult .getException ().getStackTrace ();
74+ Assertions .assertNotNull (stackTrace );
75+ Assertions .assertTrue (stackTrace .length == 0 );
76+ }
77+
78+ private Throwable buildEmptyStackTraceException () {
4579 // begin to construct a NullPointerException with empty stackTrace
4680 Throwable throwable = null ;
4781 Long begin = System .currentTimeMillis ();
@@ -59,19 +93,11 @@ public void testRecreateWithEmptyStackTraceException() {
5993 * may be there is -XX:-OmitStackTraceInFastThrow or run in Debug mode
6094 */
6195 if (throwable == null ) {
62- System .out .println ("###testRecreateWithEmptyStackTraceException fail to construct NPE" );
63- return ;
96+ System .out .println ("###buildEmptyStackTraceException fail to construct NPE" );
97+ return null ;
6498 }
6599 // end construct a NullPointerException with empty stackTrace
66100
67- RpcResult rpcResult = new RpcResult (throwable );
68- try {
69- rpcResult .recreate ();
70- fail ();
71- } catch (Throwable t ) {
72- StackTraceElement [] stackTrace = t .getStackTrace ();
73- Assertions .assertNotNull (stackTrace );
74- Assertions .assertTrue (stackTrace .length == 0 );
75- }
101+ return throwable ;
76102 }
77103}
0 commit comments