关于多@ @ Test情况下的TestNG @BeforeClass

时间:2017-08-18 03:20:35

标签: testng powermock powermockito

我在@BeforeClass中添加了initMocks和一些PowerMockito init(每个PowerMockito.when指向每个@Test)。

但是当作为TestNG测试运行时,只有一个@Test成功,其他的则失败,因为这些PowerMockito似乎不起作用。

这是测试代码。

package main.test.testng.biz;

import main.java.testng.biz.Clazzname;
import main.java.testng.dao.Dao1;
import main.java.testng.dao.Dao2;
import main.java.testng.dao.Dao3;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.junit.runner.RunWith;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Clazzname.class)
public class TestClazzname extends PowerMockTestCase {
    private Clazzname demo;
    @Mock
    private Dao1 dao1;
    @Mock
    private Dao2 dao2;
    @Mock
    private Dao3 dao3;

    @BeforeClass
    private void beforeClass() throws Exception {
        MockitoAnnotations.initMocks(this);

        PowerMockito.whenNew(Dao1.class).withNoArguments().thenReturn(dao1);
        PowerMockito.doNothing().when(dao1).set();
        PowerMockito.whenNew(Dao2.class).withNoArguments().thenReturn(dao2);
        PowerMockito.doNothing().when(dao2).set();
        PowerMockito.whenNew(Dao3.class).withNoArguments().thenReturn(dao3);
        PowerMockito.doNothing().when(dao3).set();
    }

    @BeforeMethod
    public void beforeMethod() throws Exception {
        demo = new Clazzname();
    }

    @Test
    public void test1() throws Exception {
        demo.test1();
    }

    @Test
    public void test2() throws Exception {
        demo.test2();
    }

    @Test
    public void test3() throws Exception {
        demo.test3();
    }
}

这是经过测试的代码。

package main.java.testng.biz;

import main.java.testng.dao.Dao1;
import main.java.testng.dao.Dao2;
import main.java.testng.dao.Dao3;

public class Clazzname {
    public void test1() throws Exception {
        System.out.println("test1");
        new Dao1().set();
    }

    public void test2() throws Exception {
        System.out.println("test2");
        new Dao2().set();
    }

    public void test3() throws Exception {
        System.out.println("test3");
        new Dao3().set();
    }
}

这是由经过测试的代码调用的类。

package main.java.testng.dao;

public class Dao1 {
    public void set() throws Exception {
        throw new Exception();
    }
}

这是由经过测试的代码调用的类。

package main.java.testng.dao;

public class Dao2 {
    public void set() throws Exception {
        throw new Exception();
    }
}

这是由经过测试的代码调用的类。

package main.java.testng.dao;

public class Dao3 {
    public void set() throws Exception {
        throw new Exception();
    }
}

这是结果。

test1
test2
test3
PASSED: test1
FAILED: test2
FAILED: test3

有谁知道原因?

1 个答案:

答案 0 :(得分:0)

以下事项需要在您的测试代码中修复:

  • 你需要在每个@Test之前执行powermock的模拟。因此,请更改您的@BeforeClass注释,并将其替换为@BeforeMethod
  • 每次测试都可以对Clazzname进行一次实例化。
  • 由于您未在此处使用JUnit,因此您不需要RunsWith注释。

这里是固定代码:

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@PrepareForTest(Clazzname.class)
public class TestClazzname extends PowerMockTestCase {
    private Clazzname demo;
    @Mock
    private Dao1 dao1;
    @Mock
    private Dao2 dao2;
    @Mock
    private Dao3 dao3;

    @BeforeMethod
    private void beforeMethod() throws Exception {
        MockitoAnnotations.initMocks(this);
        PowerMockito.whenNew(Dao1.class).withNoArguments().thenReturn(dao1);
        PowerMockito.doNothing().when(dao1).set();
        PowerMockito.whenNew(Dao2.class).withNoArguments().thenReturn(dao2);
        PowerMockito.doNothing().when(dao2).set();
        PowerMockito.whenNew(Dao3.class).withNoArguments().thenReturn(dao3);
        PowerMockito.doNothing().when(dao3).set();
    }

    @BeforeClass
    public void beforeClas() throws Exception {
        demo = new Clazzname();
    }

    @Test
    public void test1() throws Exception {
        demo.test1();
    }

    @Test
    public void test2() throws Exception {
        demo.test2();
    }

    @Test
    public void test3() throws Exception {
        demo.test3();
    }
}