Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 
      6 package org.mockito.internal.util;
      7 
      8 import org.assertj.core.api.Assertions;
      9 import org.junit.Test;
     10 import org.mockito.Mockito;
     11 import org.mockito.exceptions.base.MockitoException;
     12 import org.mockito.exceptions.misusing.NotAMockException;
     13 import org.mockito.internal.configuration.plugins.Plugins;
     14 import org.mockitoutil.TestBase;
     15 
     16 import java.util.ArrayList;
     17 import java.util.List;
     18 
     19 import static org.junit.Assert.*;
     20 import static org.mockito.Mockito.withSettings;
     21 
     22 @SuppressWarnings("unchecked")
     23 public class MockUtilTest extends TestBase {
     24 
     25     @Test
     26     public void should_get_handler() {
     27         List<?> mock = Mockito.mock(List.class);
     28         assertNotNull(MockUtil.getMockHandler(mock));
     29     }
     30 
     31     @Test (expected=NotAMockException.class)
     32     public void should_scream_when_not_a_mock_passed() {
     33         MockUtil.getMockHandler("");
     34     }
     35 
     36     @Test (expected=MockitoException.class)
     37     public void should_scream_when_null_passed() {
     38         MockUtil.getMockHandler(null);
     39     }
     40 
     41     @Test
     42     public void should_get_mock_settings() {
     43         List<?> mock = Mockito.mock(List.class);
     44         assertNotNull(MockUtil.getMockSettings(mock));
     45     }
     46 
     47     @Test
     48     public void should_validate_mock() {
     49         assertFalse(MockUtil.isMock("i mock a mock"));
     50         assertTrue(MockUtil.isMock(Mockito.mock(List.class)));
     51     }
     52 
     53     @Test
     54     public void should_validate_spy() {
     55         assertFalse(MockUtil.isSpy("i mock a mock"));
     56         assertFalse(MockUtil.isSpy(Mockito.mock(List.class)));
     57         assertFalse(MockUtil.isSpy(null));
     58 
     59         assertTrue(MockUtil.isSpy(Mockito.spy(new ArrayList())));
     60         assertTrue(MockUtil.isSpy(Mockito.spy(ArrayList.class)));
     61         assertTrue(MockUtil.isSpy(Mockito.mock(ArrayList.class, withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS))));
     62     }
     63 
     64     @Test
     65     public void should_redefine_MockName_if_default() {
     66         List<?> mock = Mockito.mock(List.class);
     67         MockUtil.maybeRedefineMockName(mock, "newName");
     68 
     69         Assertions.assertThat(MockUtil.getMockName(mock).toString()).isEqualTo("newName");
     70     }
     71 
     72     @Test
     73     public void should_not_redefine_MockName_if_default() {
     74         List<?> mock = Mockito.mock(List.class, "original");
     75         MockUtil.maybeRedefineMockName(mock, "newName");
     76 
     77         Assertions.assertThat(MockUtil.getMockName(mock).toString()).isEqualTo("original");
     78     }
     79 
     80     final class FinalClass {}
     81     class SomeClass {}
     82     interface SomeInterface {}
     83 
     84     @Test
     85     public void should_know_if_type_is_mockable() throws Exception {
     86         Assertions.assertThat(MockUtil.typeMockabilityOf(FinalClass.class).mockable())
     87                 .isEqualTo(Plugins.getMockMaker().isTypeMockable(FinalClass.class).mockable());
     88 
     89         assertFalse(MockUtil.typeMockabilityOf(int.class).mockable());
     90 
     91         assertTrue(MockUtil.typeMockabilityOf(SomeClass.class).mockable());
     92         assertTrue(MockUtil.typeMockabilityOf(SomeInterface.class).mockable());
     93     }
     94 }
     95