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