使用mockito使用@postconstruct为最终类编写junit

时间:2017-01-11 00:09:29

标签: java junit mockito springmockito

我正在尝试使用mockito api为最后一个类编写一个junit并使用@PostConstruct注释。下面是我到目前为止编写的示例代码。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ConfigCache.class})
public class ConfigCacheTest {

    @InjectMocks
    private ConfigCache configCache;

    @MockBean
    private ConfigDao dao;

    private Map<String,Collection<Configuration>> map;

    @Before
    public void setUp() {

        MockitoAnnotations.initMocks(this);

    }

    @After
    public void tearDown() {


    }

    @Test
    public void testInit() throws DaoException {

        map = new HashMap<String,Collection<Configuration>>();
        List<Configuration> configurations = new ArrayList<Configuration>();

        Configuration configuration = new Configuration();
        configuration.setAttributeName("a");
        configuration.setAttributeValue("20");

        configurations.add(configuration);
        configuration = new Configuration();
        configuration.setAttributeName("b");
        configuration.setAttributeValue("10");

        configurations.add(configuration);

        map.put("abc", configurations);
        when(dao.loadConfigurations()).thenReturn(map);
        assertEquals("ConfigurationCacheBuilder.init()",configurationCacheBuilder.getConfigurationcache().size(),1);

    }

}

我到目前为止的Junit是

java.lang.ExceptionInInitializerError
    at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
    at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)
    at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:30)
    at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)
    at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
    at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:12)
    at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:11)
    at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
    at org.springframework.boot.test.mock.mockito.MockReset.<clinit>(MockReset.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at java.lang.Class.getEnumConstantsShared(Class.java:3320)
    at java.lang.Class.enumConstantDirectory(Class.java:3341)
    at java.lang.Enum.valueOf(Enum.java:232)
    at sun.reflect.annotation.AnnotationParser.parseEnumValue(AnnotationParser.java:483)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:347)
    at java.lang.reflect.Method.getDefaultValue(Method.java:606)
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:128)
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
    at java.lang.reflect.Field.declaredAnnotations(Field.java:1150)
    at java.lang.reflect.Field.declaredAnnotations(Field.java:1148)
    at java.lang.reflect.Field.getDeclaredAnnotations(Field.java:1139)
    at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:207)
    at org.junit.runners.model.FrameworkField.getAnnotations(FrameworkField.java:31)
    at org.junit.runners.model.TestClass.addToAnnotationLists(TestClass.java:84)
    at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:71)
    at org.junit.runners.model.TestClass.<init>(TestClass.java:57)
    at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:83)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:138)
    at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NullPointerException
    at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)
    at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    ... 55 more

当我运行时,我收到错误

<dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-all</artifactId>
                <version>2.0.2-beta</version>
            </dependency>
and 

            <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.5.5</version>

我尝试了2个版本的mockito

{{1}}

有人能告诉我可能出现的问题吗?

1 个答案:

答案 0 :(得分:0)

您可以通过为dao变量创建包级别设置器来解决此问题。

void setConfigDao(ConfigDao configDao) {
    dao = configDao;
}

然后,请避免在课程@InjectMocks中使用ConfigCacheTest并在configCache方法中初始化您的setUp()对象。

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    configCache = new ConfigCache();
    configCache.setConfigDao(dao);
    configCache.init();
}

这样,在mockito注入@PostConstruct init()对象之前调用dao方法时,可以避免使用此NPE。