View Javadoc
1   /*
2    * SPDX-License-Identifier: Apache-2.0
3    * See LICENSE file for details.
4    *
5    * Copyright 2012-2026 hazendaz
6    *
7    * Portions of initial baseline code (getter/setter test) by Rob Dawson (CodeBox)
8    */
9   package com.codebox.instance;
10  
11  import org.junit.jupiter.api.Assertions;
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * The Class ClassInstanceAnyConstructorExceptionTest.
16   */
17  class ClassInstanceAnyConstructorExceptionTest {
18  
19      /**
20       * The Class ThrowsOnConstruct triggers InvocationTargetException.
21       */
22      static class ThrowsOnConstruct {
23  
24          /**
25           * Instantiates a new throws on construct.
26           *
27           * @param s
28           *            the s
29           */
30          public ThrowsOnConstruct(String s) {
31              throw new RuntimeException("fail");
32          }
33      }
34  
35      /**
36       * The Class AbstractWithArgs triggers InstantiationException.
37       */
38      abstract static class AbstractWithArgs {
39  
40          /**
41           * Instantiates a new abstract with args.
42           *
43           * @param s
44           *            the s
45           */
46          public AbstractWithArgs(String s) {
47          }
48      }
49  
50      /**
51       * The Class PrivateConstructor triggers IllegalAccessException.
52       */
53      static class PrivateConstructor {
54  
55          /**
56           * Instantiates a new private constructor.
57           *
58           * @param s
59           *            the s
60           */
61          private PrivateConstructor(String s) {
62          }
63      }
64  
65      /**
66       * Test any constructor invocation target exception.
67       */
68      @Test
69      void testAnyConstructorInvocationTargetException() {
70          ClassInstance<ThrowsOnConstruct> classInstance = new ClassInstance<>();
71          Assertions.assertThrows(org.opentest4j.AssertionFailedError.class,
72                  () -> classInstance.newInstance(ThrowsOnConstruct.class));
73      }
74  
75      /**
76       * Test any constructor instantiation exception.
77       */
78      @Test
79      void testAnyConstructorInstantiationException() {
80          ClassInstance<AbstractWithArgs> classInstance = new ClassInstance<>();
81          Assertions.assertThrows(org.opentest4j.AssertionFailedError.class,
82                  () -> classInstance.newInstance(AbstractWithArgs.class));
83      }
84  
85  }