View Javadoc
1   /*
2    * JavaBean Tester (https://github.com/hazendaz/javabean-tester)
3    *
4    * Copyright 2012-2025 Hazendaz.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * http://www.apache.org/licenses/LICENSE-2.0.txt
10   *
11   * Contributors:
12   *     CodeBox (Rob Dawson).
13   *     Hazendaz (Jeremy Landis).
14   */
15  package com.codebox.instance;
16  
17  import java.lang.reflect.Constructor;
18  import java.lang.reflect.InvocationTargetException;
19  
20  import org.junit.jupiter.api.Assertions;
21  import org.junit.jupiter.api.Disabled;
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.extension.ExtendWith;
24  import org.mockito.InjectMocks;
25  import org.mockito.Mock;
26  import org.mockito.Mockito;
27  import org.mockito.junit.jupiter.MockitoExtension;
28  
29  /**
30   * The Class ConstructorInstanceTest.
31   */
32  // TODO 2025-06-29 JWL Class is not mockable
33  @Disabled
34  @ExtendWith(MockitoExtension.class)
35  class ConstructorInstanceTest {
36  
37      /** The constructor instance. */
38      @InjectMocks
39      ConstructorInstance constructorInstance;
40  
41      @Mock
42      Constructor<?> mockConstructor;
43  
44      /**
45       * New instance instantiation exception.
46       *
47       * @throws InstantiationException
48       *             the instantiation exception
49       * @throws IllegalAccessException
50       *             the illegal access exception
51       * @throws InvocationTargetException
52       *             the invocation target exception
53       */
54      @Test
55      void newInstanceInstantiationException()
56              throws InstantiationException, IllegalAccessException, InvocationTargetException {
57          Mockito.when(mockConstructor.newInstance()).thenThrow(new InstantiationException());
58  
59          Assertions.assertThrows(InstantiationException.class, () -> ConstructorInstance.newInstance(mockConstructor));
60      }
61  
62      /**
63       * New instance illegal access exception.
64       *
65       * @throws InstantiationException
66       *             the instantiation exception
67       * @throws IllegalAccessException
68       *             the illegal access exception
69       * @throws InvocationTargetException
70       *             the invocation target exception
71       */
72      @Test
73      void newInstanceIllegalAccessException()
74              throws InstantiationException, IllegalAccessException, InvocationTargetException {
75          Mockito.when(mockConstructor.newInstance()).thenThrow(new IllegalAccessException());
76  
77          Assertions.assertThrows(IllegalAccessException.class, () -> ConstructorInstance.newInstance(mockConstructor));
78      }
79  
80      /**
81       * New instance invocation target exception.
82       *
83       * @throws InstantiationException
84       *             the instantiation exception
85       * @throws IllegalAccessException
86       *             the illegal access exception
87       * @throws InvocationTargetException
88       *             the invocation target exception
89       */
90      @Test
91      void newInstanceInvocationTargetException()
92              throws InstantiationException, IllegalAccessException, InvocationTargetException {
93          Mockito.when(mockConstructor.newInstance()).thenThrow(new InvocationTargetException(new Exception()));
94  
95          Assertions.assertThrows(InvocationTargetException.class,
96                  () -> ConstructorInstance.newInstance(mockConstructor));
97      }
98  
99  }