英文:
JUnit parameterized and non-parameterized tests with PowerMockito
问题
以下是翻译好的内容:
我正在尝试在同一个测试类中运行JUnit参数化测试和非参数化测试。但我遇到了各种错误。是否有人之前尝试过这样做,并且他们是否成功了?我知道其他的运行器需要与@PowerMockRunnerDelegate
一起使用才能正确运行。所以这是我想出来的解决方法:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Enclosed.class)
@PrepareForTest(Some.class)
@PowerMockIgnore("javax.management.*")
public class TestClass {
@PowerMockRunnerDelegate(Parameterized.class)
public static class ParameterizedTests {
}
@Test
public void nonParameterizedTestOne() {
}
@Test
public void nonParameterizedTestTwo() {
}
}
但是我遇到了错误:
测试类应该有一个公共的零参数构造函数
如果不使用PowerMock,这种情况可以很容易地处理如下:
@RunWith(Enclosed.class)
public class TestClass {
@RunWith(Parameterized.class)
public static class ParameterizedTests {
}
@Test
public void nonParameterizedTestOne() {
}
@Test
public void nonParameterizedTestTwo() {
}
}
但我肯定想使用PowerMock。有什么解决方案吗?
英文:
So I am trying to run JUnit parameterized tests along with non-parameterized tests in the same test class. But I am running into one error or the other. Has anyone tried this before and were they successful in doing so? I know other runners need to be used with the @PowerMockRunnerDelegate
in order to run correctly. So here's what I came up with:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Enclosed.class)
@PrepareForTest(Some.class)
@PowerMockIgnore("javax.management.*")
public class TestClass {
@PowerMockRunnerDelegate(Parameterized.class)
public static class ParameterizedTests {
}
@Test
public void nonParameterizedTestOne() {
}
@Test
public void nonParameterizedTestTwo() {
}
}
But I get the error:
Test class should have exactly one public zero-argument constructor
Without powermock, this situation can be easily handled with:
@RunWith(Enclosed.class)
public class TestClass {
@RunWith(Parameterized.class)
public static class ParameterizedTests {
}
@Test
public void nonParameterizedTestOne() {
}
@Test
public void nonParameterizedTestTwo() {
}
}
But I would definitely like to use powermock. Any solutions?
答案1
得分: 1
我遇到了相同的问题,这对我有效:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Enclosed.class)
@PrepareForTest({Some.class})
public class TestClass {
private static void setUpOnceForAllTests() {
}
private static void setUpForEveryTest() {
}
public static class SingleTests {
// 为所有单元测试设置一次
@BeforeClass
public static void setUpBeforeClass() {
setUpOnceForTests();
}
// 为每个单元测试设置
@Before
public void setUp() {
setUpForEveryTest();
}
@Test
public void nonParameterizedTestOne() {
}
@Test
public void nonParameterizedTestTwo() {
}
}
@PowerMockRunnerDelegate(Parameterized.class)
public static class ParameterizedTests {
// 为所有参数化测试设置一次
@BeforeClass
public static void setUpBeforeClass() {
setUpOnceForTests();
}
// 为每个参数化测试设置
@Before
public void setUp() {
setUpForEveryTest();
}
@Parameterized.Parameters
public static Collection<Enum> param() {
return new ArrayList<>(Arrays.asList(Enum.values()));
}
@Parameterized.Parameter
public int param;
@Test
public void parameterizedTestOne() {
}
@Test
public void parameterizedTestTwo() {
}
}
}
英文:
I had the same issue, this worked for me:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Enclosed.class)
@PrepareForTest({Some.class})
public class TestClass {
private static void setUpOnceForAllTests() {
}
private static void setUpForEveryTest() {
}
public static class SingleTests {
// Setup once for all single tests
@BeforeClass
public static void setUpBeforeClass() {
setUpOnceForTests();
}
// Setup for each and every single test
@Before
public void setUp() {
setUpForEveryTest();
}
@Test
public void nonParameterizedTestOne() {
}
@Test
public void nonParameterizedTestTwo() {
}
}
@PowerMockRunnerDelegate(Parameterized.class)
public static class ParameterizedTests {
// Setup once for all parameterized test
@BeforeClass
public static void setUpBeforeClass() {
setUpOnceForTests();
}
// Setup for each and every parameterized test
@Before
public void setUp() {
setUpForEveryTest();
}
@Parameterized.Parameters
public static Collection<Enum> param() {
return new ArrayList<>(Arrays.asList(Enum.values()));
}
@Parameterized.Parameter
public int param;
@Test
public void parameterizedTestOne() {
}
@Test
public void parameterizedTestTwo() {
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论