错误:类型不匹配:无法从Test转换为Annotation

时间:2015-10-11 13:23:11

标签: java junit mockito powermock powermockito

我是junit Mockito框架的新手,我使用powermock框架嘲笑了依赖注入,但我在@Test注释的eclipse中出错了错误是“类型不匹配:无法从Test转换为注释“

package org.singh.util.MockitoDemo;

import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;

import org.junit.*;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ExampleUtil.class, ExamplePojo.class})
public class ExamplePojoTest{

    @Test
    public void testMethodMakingPrivateMethodCall() throws Exception {
        ExamplePojo spyExamplePojo = PowerMockito.spy(new ExamplePojo());
        when(spyExamplePojo, method(ExamplePojo.class, "privateMethod", String.class)).withArguments(anyString()).thenReturn("test test");
        String result = spyExamplePojo.methodMakingPrivateMethodCall("test");
        Assert.assertEquals("test test", result);
    }

}

maven依赖是

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

1 个答案:

答案 0 :(得分:6)

当您看到此错误时,表示您在同一个包中有一个类名为“Test”。

enter image description here

只需重命名该类名称即可正常工作!!!!

相关问题