Error in junit while mocking

后端 未结 2 772
鱼传尺愫
鱼传尺愫 2020-12-18 13:00

I am new to Junit,Below is the junit code which i am running.

package com.de.base.util.general;
import static org.junit.Assert.*;
import static org.mockito.M         


        
相关标签:
2条回答
  • 2020-12-18 13:18

    Here is my working code:

    import static org.junit.Assert.assertEquals;
    
    import java.util.HashMap;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mockito;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(CollectionUtil.class)
    public class TestHarnesTest {
        @InjectMocks
        TestHarnes w_res = new TestHarnes();
    
        @Before
        public void before() {
            PowerMockito.mockStatic(CollectionUtil.class);
        }
    
        @Test
        public void test_removeHashedSettings() throws Exception {
            HashMap<String, String> w_abc = new HashMap<String, String>();
            w_abc.put("abc", "89");
            // CollectionUtil mock = org.mockito.Mockito.mock(CollectionUtil.class);
            // PowerMockito.mockStatic(CollectionUtil.class,w_abc);
            PowerMockito.when(CollectionUtil.createHashMap(Mockito.eq("abc:89"), Mockito.eq(":"))).thenReturn(w_abc);
            assertEquals("abc:89:", TestHarnes.removeHashedSettings("1", "abc:89", ":"));
        }
    }
    

    and the TestHarnes class

    public class TestHarnes {
    
        public static String removeHashedSettings(final String key, final String a_settings, final String deilimiter) throws Exception {
            if (!(key != null && key.trim().length() > 0)) {
                return a_settings;
            }
            if (!(a_settings != null && a_settings.trim().length() > 0)) {
                return a_settings;
            }
    
            HashMap hSettings = CollectionUtil.createHashMap(a_settings, deilimiter);
            hSettings.remove(key);
            return getSettingFromHash(hSettings, deilimiter);
        }
    
        private static String getSettingFromHash(final HashMap hSettings, final String deilimiter) {
            return "";
        }
    
    }
    
    0 讨论(0)
  • 2020-12-18 13:26

    You don't use the PowerMock runner :

    @RunWith(PowerMockRunner.class)
    

    Mockito cannot mock static method but PowerMock does.

    And you should mock the class with static method :

    PowerMockito.mockStatic(CollectionUtil.class);
    

    Anyway a better design is replacing the static method by an instance method.
    Static methods are not naturally testable and force to create complex and not readable workaround.
    For example, look at the complexity of the mix of dependencies required to test a simple class.

    You should keep the use of static methods as helper that don't perform core logic of the domain and that don't need to be mocked.

    When the methods perform core logic of the domain as it is the case for the createHashMap() static method, you very probably need to mock it to not create side effects between dependent classes during the tests.
    And as you have noticed : mocking static methods is really clumsy as static methods are really not designed to be overriden.

    Besides, for core logic of the domain, you should have the ability to take advantage of the OOP (inheritancy, polymorphism, design patterns, etc... ), how to achieve it with static methods ? –

    For legacy code we cannot really not change, it is acceptable but otherwise, no it doesn't and you should refactor your code.

    0 讨论(0)
提交回复
热议问题