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 java.lang.reflect.InvocationTargetException;
12  
13  import org.junit.jupiter.api.Assertions;
14  import org.junit.jupiter.api.Disabled;
15  import org.junit.jupiter.api.Test;
16  import org.junit.jupiter.api.extension.ExtendWith;
17  import org.mockito.InjectMocks;
18  import org.mockito.Mock;
19  import org.mockito.Mockito;
20  import org.mockito.junit.jupiter.MockitoExtension;
21  
22  /**
23   * The Class ClassInstanceTest.
24   */
25  // TODO 2025-06-29 JWL Class is not mockable
26  @Disabled
27  @ExtendWith(MockitoExtension.class)
28  class ClassInstanceTest {
29  
30      /** The class instance. */
31      @InjectMocks
32      ClassInstance<Object> classInstance;
33  
34      /** The mock clazz. */
35      @Mock
36      Class<Object> mockClazz;
37  
38      /**
39       * New instance instantiation exception.
40       *
41       * @throws InstantiationException
42       *             the instantiation exception
43       * @throws IllegalAccessException
44       *             the illegal access exception
45       * @throws NoSuchMethodException
46       *             the no such method exception
47       * @throws InvocationTargetException
48       *             the invocation target exception
49       */
50      @Test
51      void newInstanceInstantiationException()
52              throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
53          Mockito.when(mockClazz.getDeclaredConstructor()).thenReturn(Object.class.getDeclaredConstructor());
54          Mockito.when(mockClazz.getDeclaredConstructor().newInstance()).thenThrow(new InstantiationException());
55  
56          Assertions.assertThrows(InstantiationException.class, () -> this.classInstance.newInstance(this.mockClazz));
57      }
58  
59      /**
60       * New instance illegal access exception.
61       *
62       * @throws InstantiationException
63       *             the instantiation exception
64       * @throws IllegalAccessException
65       *             the illegal access exception
66       * @throws NoSuchMethodException
67       *             the no such method exception
68       * @throws InvocationTargetException
69       *             the invocation target exception
70       */
71      @Test
72      void newInstanceIllegalAccessException()
73              throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
74          Mockito.when(mockClazz.getDeclaredConstructor()).thenReturn(Object.class.getDeclaredConstructor());
75          Mockito.when(mockClazz.getDeclaredConstructor().newInstance()).thenThrow(new IllegalAccessException());
76  
77          Assertions.assertThrows(InstantiationException.class, () -> this.classInstance.newInstance(this.mockClazz));
78      }
79  
80  }