美文网首页
junit 单元测试

junit 单元测试

作者: 代瑶 | 来源:发表于2018-09-03 13:54 被阅读0次

    采用 Junit + Mockito+ PowerMock 来实现

    package com.telenav.arp.asset.app;
    
    import android.content.res.AssetManager;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.JsonObject;
    import com.telenav.arp.data.CategoryNode;
    import com.telenav.arp.sdk.asset.AssetFileUtil;
    import com.telenav.arp.vehicle.VehicleCalibrations;
    import com.telenav.arp.vehicle.VehicleInfoFactory;
    
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.mockito.MockitoAnnotations;
    import org.mockito.internal.util.reflection.FieldSetter;
    import org.mockito.internal.util.reflection.Whitebox;
    import org.mockito.invocation.InvocationOnMock;
    import org.mockito.stubbing.Answer;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PowerMockIgnore;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    import org.powermock.modules.junit4.PowerMockRunnerDelegate;
    import org.powermock.modules.junit4.rule.PowerMockRule;
    import org.robolectric.RobolectricTestRunner;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import static org.junit.Assert.assertEquals;
    
    //@RunWith(PowerMockRunner.class)
    //@PowerMockRunnerDelegate(RobolectricTestRunner.class)
    
    @RunWith(RobolectricTestRunner.class)
    @PrepareForTest(AssetFileUtil.class)
    @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
    
    public class CategoryInfoAssetTest {
        private CategoryInfoAsset mCategoryInfoAsset;
    
        //避免两个RunWith
        @Rule
        public PowerMockRule rule = new PowerMockRule();
    
        @BeforeClass
        public static void testInit() {
            MockitoAnnotations.initMocks(CategoryInfoAssetTest.class);
        }
    
        @Before
        public void testBefore() {
            mCategoryInfoAsset = CategoryInfoAsset.getInstance();
        }
    
        //反射实现或者通过PowerMock来实现对私有方法进行赋值
        private void setFieldValue(String fieldName, Object fieldObject) {
            try {
                Field mField = CategoryInfoAsset.class.getDeclaredField(fieldName);
    
                FieldSetter fieldSetter = new FieldSetter(mCategoryInfoAsset, mField);
                fieldSetter.set(fieldObject);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
    
        @Test
        public void getCategories() {
            VehicleCalibrations mVehicleCalibrations = Mockito.mock(VehicleCalibrations.class);
            VehicleInfoFactory.initVehicleCalibrations(mVehicleCalibrations);
    
            PowerMockito.mockStatic(AssetFileUtil.class);
            PowerMockito.when(AssetFileUtil.getAssetForString(null, "app/category/na/pin_on_map.json"))
                    .thenAnswer(new Answer<String>() {
                        @Override
                        public String answer(InvocationOnMock invocation) throws Throwable {
                            ArrayList<CategoryNode> categoryNodes = new ArrayList<>();
    
                            CategoryNode mNode = new CategoryNode();
                            categoryNodes.add(mNode);
    
                            GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls();
                            Gson gson = gsonBuilder.create();
    
                            return gson.toJson(categoryNodes);
                        }
                    });
    
            ArrayList<CategoryNode> categories = mCategoryInfoAsset.
                    getCategories(CategoryInfoAsset.CategoryType.hotCategory);
        }
    
        @Test
        public void clearPOICategories() {
            mCategoryInfoAsset.clearPOICategories();
        }
    
        @Test
        public void getCategoryIconResId() {
            HashMap<String, Integer> hashMap = new HashMap<>();
            hashMap.put("categoryIcon", 1001);
    
            setFieldValue("pinOnMapResourceIdCache", hashMap);
    
            int categoryIcon = mCategoryInfoAsset.getCategoryIconResId(
                    "categoryIcon",
                    true,
                    CategoryInfoAsset.CategoryType.pinOnMap,
                    true);
    
            assertEquals(categoryIcon, 1001);
    
        }
    
        @Test
        public void getHMICategoryByHotCategoryQueryId() {
        }
    
        @Test
        public void getHMICategory() {
        }
    
        @Test
        public void getHMICategory1() {
        }
    
        @Test
        public void getCategoryNodeByHMICategoryId() {
        }
    
        @Test
        public void getCategoryNodeDisplayLabel() {
        }
    
        @Test
        public void getCategoryNodeDisplayLabel1() {
        }
    
        @Test
        public void getCategoryNodeLogLabel() {
        }
    
        @Test
        public void getCategoryNodeLogLabel1() {
        }
    
        @Test
        public void isGasCategory() {
        }
    
        @Test
        public void getCategoryStyle() {
        }
    
        @Test
        public void getDisplayStyleCategoryName() {
        }
    
        @Test
        public void getQueryCategoryIds() {
        }
    
        @Test
        public void getChildrenCategoryIds() {
        }
    
        @Test
        public void getPoiNamesOnMapConfigPriority() {
        }
    
        @Test
        public void getCategoryDisplayPriority() {
        }
    
        @Test
        public void getCategorySearchPriority() {
        }
    
        @Test
        public void getCategoryLevelLimitUpper() {
        }
    
        @Test
        public void isLowPriorityCategory() {
        }
    
        @Test
        public void needRemoveBranchName() {
        }
    }
    

    相关文章

      网友评论

          本文标题:junit 单元测试

          本文链接:https://www.haomeiwen.com/subject/jqibwftx.html