PowerMock Mockito忽略junit FixMethodOrder

时间:2015-02-17 12:25:09

标签: java mocking mockito junit4 powermock

我在这里遇到一些问题,而且我不知道如何解决它。

我有一个必须为某些JSF bean进行测试的类。 为了实现这一点,我使用PowerMock和Mockito来模拟FacesContext,RequestContext和JSF bean中使用的另一个静态方法。

@PrepareForTest(ClassWithStaticMethods.class)
@RunWith(PowerMockRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTestingClass extends SomeTestBaseClass{

    @BeforeClass
    public static void init() throws Exception{
        //mocking the FacesContext and others
    }

    @Test
    public void test0001Create(){}

    @Test
    public void test0002Edit(){}

    @Test
    public void test0003Delete(){}
}

SomeTestBaseClass,没什么复杂的。

public abstract class SomeTestBaseClass {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        //...
    }
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        //...
    }
}

问题是忽略了测试的顺序(即使使用FixMethodOrder注释)。如果我删除PowerMockRunner(和RunWith注释),则保留顺序,但静态(和无效)方法的模拟不起作用。 但是使用PowerMockRunner离开该类时,注释@FixMethodOrder将完全被忽略。

我甚至尝试使用MockitoJUnitRunner,这里保留了测试顺序,但是静态(和无效)方法的模拟还没有完成。

有谁知道为什么会这样?

由于

4 个答案:

答案 0 :(得分:1)

像解决方法一样:创建一个新方法(比如说'testAll'),只为此放置@Test注释(从其余方法中删除@Test注释),然后调用你的测试方法。注释方法。

很脏,但是有效。

@PrepareForTest(ClassWithStaticMethods.class)
@RunWith(PowerMockRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTestingClass extends SomeTestBaseClass{

    @BeforeClass
    public static void init() throws Exception{
        //mocking the FacesContext and others
    }

    @Test
    public void testAll(){
        this.test0001Create();
        this.test0002Edit();
        this.test0003Delete();
    }

    public void test0001Create(){}

    public void test0002Edit(){}

    public void test0003Delete(){}
}

答案 1 :(得分:0)

我不知道为什么它不能与PowerMockRunner一起使用,而是可以使用PowerMockRuke

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTestingClass extends SomeTestBaseClass {
    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @BeforeClass
    public static void init() throws Exception {
        // mocking the FacesContext and others
    }
    @Test
    public void test0001Create() {
    }

    @Test
    public void test0002Create() {
    }

    @Test
    public void test0003Create() {
    }
}

答案 2 :(得分:0)

请尝试更改顺序:

  1. @PrepareForTest(ClassWithStaticMethods.class)
  2. @echo off set myscrpt="C:\path\containing spaces\mypythonscript.py" set mypath1="C:\path\containing spaces\inputs" set mypath2="C:\path\containing spaces\outputs" pushd C:\Python27 Python %myscrpt% %mypath1% %mypath2% pause>NUL exit
  3. public function defaultForUser() { return $this->belongsTo(User::class, 'id', 'default_payment_method_id'); } <h3>Payment methods</h3> <table border="1"> <tr><th></th><th></th><th></th></tr> @foreach($user->payment_methods()->withCount('defaultForUser')->get() as $payment_method) <tr> <td> <div><strong>id:</strong> {{$payment_method->id }}</div> <div><strong>gateway id:</strong> {{ $payment_method->braintree_id }}</div> </td> <td> @if($payment_method ->type == PaymentMethod::PAYPAL_ACCOUNT) <div><strong>Paypal: </strong> {{$payment_method -> paypal_email}}</div> @else <div><strong>Card: </strong>{{ $payment_method -> card_brand }} **** **** **** {{$payment_method -> card_last_four}}</div> @endif <td> @if ($payment_method -> default_for_user_count) <strong style = "color:green">Default</strong> @endif </td> </td> </tr> @endforeach </table>

答案 3 :(得分:0)

我有同样的问题。 我通过使用@PowerMockRunnerDelegate批注解决了它:

在我的测试课注释中:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(PowerMockRunner.class)

我添加了@PowerMockRunnerDelegate(JUnit4.class)

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(JUnit4.class)

它们现在按预期顺序运行。