JUnit参数化和非参数化测试与PowerMockito。

huangapple go评论91阅读模式
英文:

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&lt;Enum&gt; param() {
return new ArrayList&lt;&gt;(Arrays.asList(Enum.values()));
}
@Parameterized.Parameter
public int param;
@Test
public void parameterizedTestOne() {
}
@Test
public void parameterizedTestTwo() {
}
}
}

huangapple
  • 本文由 发表于 2020年4月5日 20:19:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定