Junit测试未与PowerMockito一起运行

时间:2020-07-26 05:02:33

标签: java junit mockito powermockito maven-surefire-plugin

我正在尝试使用Powermockito 1.6.6进行Junit 4.12测试的一些行为,我正在使用Maven Surefire插件运行测试。添加注释@RunWith(PowerMockRunner.class)后,甚至没有触发测试用例。但是在正确触发之前。不确定是否需要其他任何方式使它运行

mvn test "-Dtest=MyTestFile" -DfailIfNoTests=false -Dsurefire.useFile=false -DtrimStackTrace=false
@RunWith(PowerMockRunner.class)
@PrepareForTest(BeanServiceLocator.class)
public class AvailableAPsControlTest extends ACPTest {

    private static IdentityContainer hz_icontainer;
    private static MemCache memCache;
    static final String CUSTID = "20200620";
    private String mac1 = Testing.generateRandomMac();
    private String mac2 = Testing.generateRandomMac();
    private String mac3 = Testing.generateRandomMac();
    private String mac4 = Testing.generateRandomMac();

    @BeforeClass
    public static void onlyOnce() throws Exception {
        ACPTest.setUpBeforeClass();
        ACPTestDatabase.init();
        ACPTestDatabase.startDatabaseService();
        Testing.setLogLevel(Testing.LogLevel.INFO);

    }

    @After
    public void tearDown() throws Exception {
        Testing.teardownAfterClass();
    }

    @Test
    public void getApSiteComboTest() throws Exception {
        //Given
        Testing.setCustId(CUSTID);
    
        AvailableAPsControl apsControl = new AvailableAPsControl();
        DeviceLicenseCheckServiceImpl ds = (DeviceLicenseCheckServiceImpl)BeanServiceLocator.lookup(DeviceLicenseCheckService.class);
        //ds = spy(ds);
        DeviceLicenseCheckServiceImpl ds1 = mock(DeviceLicenseCheckServiceImpl.class);
        apsControl = spy(apsControl);

        APsModel apsModel = createApsModel(Arrays.asList(mac1, mac2,mac3,mac4));


        AtomicInteger total = new AtomicInteger(0);

        //When getApSiteCombo called
        //when(apsControl.getSiteApListFromMonitoring(CUSTID, CUST_CSITE, 100, 0)).thenReturn(apsModel);
        doReturn(apsModel).when(apsControl).getSiteApListFromMonitoring(CUSTID, CUST_CSITE, 100, 0);
        log.warn("AP state {}",testAp1.getState());
        List<AccessPoint> vrfAps = apsControl.getApSiteCombo(params, total);
        //Then
        assertEquals(4, total.get());
        assertEquals(1, vrfAps.size());

        AccessPoint testAp4 = com.airwave.model.testing.Testing.getTestObject(AccessPoint.class, "name", "test-Ap4", "id", CUSTID + "__" + mac4 ,"state",5);
        testAp4.setState(NEEDSLICENSECHECK);
        hz_icontainer.put(testAp4);
        PowerMockito.mockStatic(BeanServiceLocator.class);
        BDDMockito.given(BeanServiceLocator.lookup(DeviceLicenseCheckService.class)).willReturn(ds);
        //PowerMockito.whenNew(DeviceLicenseCheckServiceImpl.class).withNoArguments().thenReturn((DeviceLicenseCheckServiceImpl)ds1);
        doNothing().doAnswer(new Answer() {
            public Object answer(InvocationOnMock invocation) {
            AccessPoint ap = (AccessPoint)invocation.getArguments()[0];
            ap.setState(PROVISIONED);
            return null;
                }}).doNothing().when(ds).updateLicenseStateForAp(isA(AccessPoint.class));
        vrfAps = apsControl.getApSiteCombo(params, total);
        assertEquals(4,total.get());
        assertEquals(2, vrfAps.size());
        assertEquals(PROVISIONED,testAp4.getState());
        vrfAps = apsControl.getApSiteCombo(params, total);
        assertEquals(4,total.get());
        assertEquals(3, vrfAps.size());
    }

0 个答案:

没有答案