简体   繁体   English

未调用模拟方法

[英]mocked method not being invoked

I am using testng with Mockito and due to a restriction I cannot use PowerMock.我正在将 testng 与 Mockito 一起使用,但由于限制,我无法使用 PowerMock。 When i try to run my test, for some reason the real method is called instead of the mock!当我尝试运行我的测试时,由于某种原因,调用的是真正的方法而不是模拟! I have added a wrapper class around the class which has static methods.我在具有静态方法的类周围添加了一个包装类。 I know from the documentation that Mockito does not support static methods.我从文档中知道 Mockito 不支持静态方法。 So, as a way over it, I have written a simple wrapper which just calls the static method in that class.因此,作为一种解决方法,我编写了一个简单的包装器,它只调用该类中的静态方法。

public class BatTrafficManagerStatsHelperTest {

    @Mock
    private MtsConfFactoryWrapper mtsConfFactoryWrapper = new MtsConfFactoryWrapper();

    private static final String EXPECTED_JSON_STR = "{"
            + "         \"duration\": 100,"
            + "         \"timestamp\": \"1970-01-01T00:00:00\","
            + "         \"cellMetrics\": {"
            + "           \"enb1_cell1\": {"
            + "             \"handoverAttempts\": 2,"
            + "             \"rrcConnectionAttempts\": 1"
            + "           }"
            + "         },"
            + "         \"trafficProfileMetrics\": {"
            + "           \"voipSpeech_2PDN\": {"
            + "             \"srMtSuccess\": 4,"
            + "             \"numberOfUEs\": 100,"
            + "             \"srMoSuccess\": 3,"
            + "             \"attachSuccess\": 5,"
            + "             \"rrcConnectionAttempts\": 1"
            + "           }"
            + "         },"
            + "         \"ueGroupMetrics\": {"
            + "           \"ueGroup1\": {"
            + "             \"handoverAttempts\": 2,"
            + "             \"numberOfUEs\": 100,"
            + "             \"rrcConnectionAttempts\": 1"
            + "           }"
            + "         }"
            + "       }";

    @BeforeMethod
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    /**
     * getBstmString()
     * Setup mock, tests getBstmString() and verify result
     *
     * @throws Exception
     */
    @Test
    public void testGetBtmString() throws Exception {
        final Date date = Mockito.mock(Date.class);
        Mockito.when(date.getTime()).thenReturn(0L);

        final Map<OptimizedRoute, List<StatsQueue>> ueStatsTrackerMock = getUeStatsTrackerMock();
        final Map<Enb, List<OptimizedCellStatsQueue>> cellStatsTrackerMock = getCellStatsTrackerMock();
        final MtsConfBuilder confBuilderMock = Mockito.mock(MtsConfBuilder.class);
        final Tracker trackerMock = Mockito.mock(Tracker.class);

        Mockito.when(mtsConfFactoryWrapper.getConfBuilderr()).thenReturn(confBuilderMock);
        Mockito.when(confBuilderMock.getTracker()).thenReturn(trackerMock);
        Mockito.when(trackerMock.getUeStatsTracker()).thenReturn(
                ueStatsTrackerMock);
        Mockito.when(trackerMock.getCellStatsTracker()).thenReturn(
                cellStatsTrackerMock);
        Mockito.when(trackerMock.getConfiguredTrafficProfiles())
                .thenReturn(
                        getFakeTrafficProfilesList());

        BatTrafficManagerStatsHelper btmStatsHelper = new BatTrafficManagerStatsHelper(date);
        String result = btmStatsHelper.getBtmString(TIME).replace("JsonDataForBatTrafficManagerRegex:", "");

        JsonParser parser = new JsonParser();
        assertEquals(parser.parse(result), parser.parse(EXPECTED_JSON_STR));
    }

}

The orginal class under test looks like this被测试的原始类看起来像这样

public class BatTrafficManagerStatsHelper {

    private final Date date;

    /*
     * Constants for BAT Traffic Manager log parsing
     */
    private static final ImmutableMap<String, String> AHL_STATS_CONSTANTS_TO_BTM_CONSTANTS = ImmutableMap.of(
            BatAeroflexHLStatsConstants.RRC_CONNECTION_REQUEST_ATTEMPTS, "rrcConnectionAttempts",
            BatAeroflexHLStatsConstants.HANDOVER_ATTEMPTS, "handoverAttempts",
            BatAeroflexHLStatsConstants.SERVICE_REQ_MO_SUCCESS, "srMoSuccess",
            BatAeroflexHLStatsConstants.SERVICE_REQ_MT_SUCCESS, "srMtSuccess",
            BatAeroflexHLStatsConstants.ATTACH_SUCCESS, "attachSuccess");
    private static final String NUMBER_OF_UES = "numberOfUEs";

    /**
     * Constructor
     *
     * @param date
     */
    public BatTrafficManagerStatsHelper(Date date) {
        this.date = new Date(date.getTime());
    }

    /**
     * Extracts log data and returns it in a format suited
     * for parsing in {@literal BAT Traffic Manager}.
     *
     * @param elapsedTime
     * @return log data for BAT Traffic Manager
     */
    public String getBtmString(long elapsedTime) {
        MtsConfFactoryWrapper mtsConfFactoryWrapper = new MtsConfFactoryWrapper();
        Map<OptimizedRoute, List<StatsQueue>> ueGroupTracker = mtsConfFactoryWrapper.getConfBuilderr().getTracker()
                .getUeStatsTracker();
        Map<Enb, List<OptimizedCellStatsQueue>> cellTracker = mtsConfFactoryWrapper.getConfBuilderr().getTracker()
                .getCellStatsTracker();

        BatTrafficManagerStatsHelperJson json = new BatTrafficManagerStatsHelperJsonBuilder()
                .setDuration(elapsedTime)
                .setTimestamp(getTimeStamp())
                .setCellMetrics(getCellMetrics(cellTracker))
                .setTrafficProfileMetrics(getTrafficProfileMetrics(ueGroupTracker))
                .setUeGroupMetrics(getUeGroupMetrics(ueGroupTracker))
                .build();

        return "JsonDataForBatTrafficManagerRegex:" + getJsonString(json);
    }

}

public class MtsConfFactoryWrapper  {
    public MtsConfFactoryWrapper() {}

    public MtsConfBuilder getConfBuilderr() {
        return MtsConfFactory.getConfBuilder();
    }
}

Use this in your test class,在您的测试类中使用它,

@Mock
private MtsConfFactoryWrapper mtsConfFactoryWrapper;

instead of代替

@Mock
private MtsConfFactoryWrapper mtsConfFactoryWrapper = new MtsConfFactoryWrapper();

You shouldn't assigned a new object to mtsConfFactoryWrapper variable.您不应将新对象分配给mtsConfFactoryWrapper变量。 A mock object will be assigned to mtsConfFactoryWrapper variable when MockitoAnnotations.initMocks(this);MockitoAnnotations.initMocks(this);时,一个模拟对象将被分配给mtsConfFactoryWrapper变量MockitoAnnotations.initMocks(this); get called.被调用。

In your method you are always creating a new object like below,在您的方法中,您总是在创建一个新对象,如下所示,

MtsConfFactoryWrapper mtsConfFactoryWrapper = new MtsConfFactoryWrapper();

So even you mocked the object in your test, when the method get called, it will again create a new object.因此,即使您在测试中模拟了该对象,当该方法被调用时,它也会再次创建一个新对象。 Try to inject that to the class instead of creating a new instance inside your method.尝试将其注入到类中,而不是在您的方法中创建一个新实例。

Your class should look like this,你的班级应该是这样的,

public class BatTrafficManagerStatsHelper {

    private MtsConfFactoryWrapper mtsConfFactoryWrapper;

    public BatTrafficManagerStatsHelper(MtsConfFactoryWrapper mtsConfFactoryWrapper) {
        this.mtsConfFactoryWrapper = mtsConfFactoryWrapper;
    }

    // other stuffs
}

In your test class when you create the object of BatTrafficManagerStatsHelper , pass your mock object to the constructor as below,在您的测试类中,当您创建BatTrafficManagerStatsHelper对象时,将您的模拟对象传递给构造函数,如下所示,

public class BatTrafficManagerStatsHelperTest {
    @Test
    public void testGetBtmString() throws Exception {
        //your code

        BatTrafficManagerStatsHelper btmStatsHelper = new BatTrafficManagerStatsHelper(passYourMockedObjectHere);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM