【问题标题】:About TestNG @BeforeClass in multi@Test situation关于多@Test情况下的TestNG @BeforeClass
【发布时间】:2017-08-18 03:20:34
【问题描述】:

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

但是当作为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

有人知道原因吗?

【问题讨论】:

  • 错误信息是什么?
  • 例如,
     在 BeforeClass { PowerMockito.doNothing().when(dao).set(Mockito.any(bean.class)); } 在测试 { fun() { dao.set(bean); } } 成功的Test可以忽略dao.set,但其他人进入dao.set。我想要的是所有Test都可以忽略它。无论如何,如果我将 mock init 放在 BeforeMethod 中,它就可以工作!
  • @Monica - 你能帮忙分享一个完整的例子吗?可以执行它来重现问题?此外,如果可能,请包括您正在使用的模拟版本(可能作为 maven 依赖项)。通过更新您的问题来包括所有这些。
  • @KrishnanMahadevan - 我使用 org.mockito1.10.19, org.powermock1.6.5 org.testng6.1.1。我提供上面的代码。
  • @JeroenHeier - 我提供了上面的代码。Demo.test* 中只有一个 dao* 可以根据 mock 规则做任何事情,其他的进入 dao.set。

标签: testng powermock powermockito


【解决方案1】:

需要在您的测试代码中修复以下问题:

  • 您需要在每个 @Test 之前执行 powermock 模拟。所以请更改您的 @BeforeClass 注释并将其替换为 @BeforeMethod
  • Clazzname 的实例化可以在每次测试中完成一次。
  • 您不需要RunsWithannotation,因为您在这里没有使用JUnit。

这是固定代码:

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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多