如何在JUnit测试用例中使用BeanPostProcessor

时间:2018-06-11 11:42:38

标签: java spring spring-boot spring-integration integration-testing

我尝试使用以下代码改进Spring Integration Test的启动。

public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    private Class<?>[] exclusionList;

    public LazyInitBeanFactoryPostProcessor() {
    }

    public LazyInitBeanFactoryPostProcessor(Class<?>[] exclusionList) {
        this.exclusionList = exclusionList;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        // Iterate over all bean, mark them as lazy if they are not in the
        // exclusion list.
        for (String beanName : beanFactory.getBeanDefinitionNames()) {
            if (isLazy(beanName, beanFactory)) {
                BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
                definition.setLazyInit(true);
            }
        }
    }

    private boolean isLazy(String beanName, ConfigurableListableBeanFactory beanFactory) {
        if (exclusionList == null || exclusionList.length == 0) {
            return true;
        }
        for (Class<?> clazz : exclusionList) {
            if (beanFactory.isTypeMatch(beanName, clazz)) {
                return false;
            }
        }
        return true;
    }

}

但是我坚持这个

java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    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(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    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)

供参考https://github.com/spring-projects/spring-boot/issues/9685

更新: - 正如所建议的那样,我尝试但它仍然会引发相同的错误,如果我删除BeanFactoryPostProcessor,它就可以正常工作

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@AutoConfigureDataMongo
@AutoConfigureMockMvc
public class SocialMessageTest {
@TestConfiguration
protected static class ServiceTestConfiguration {

    @Bean
    public BeanFactoryPostProcessor lazyBeanPostProcessor() {
        return new ServiceTestLazyBeanFactoryPostProcessor();
    }

    static private class ServiceTestLazyBeanFactoryPostProcessor extends LazyInitBeanFactoryPostProcessor {
        public ServiceTestLazyBeanFactoryPostProcessor() {
            super(new Class<?>[] { SocialMediaController.class });
        }
    }

    @Test
    public void TestEditMessage_WithValidFlow() throws JsonProcessingException {
        MessageEntity messageEntityInput = new MessageEntity();
        messageEntityInput.setMessageText(testMessageText);
        messageEntityInput.setVisibility("Public");
        messageEntityInput.setStatus(1);
        mongoTemplate.save(messageEntityInput);
        MessageView messageView = new MessageView();
        messageView.setMessageText("Hello! Mansi");
        messageView.setVisibility("Public");
        HttpEntity<MessageView> httpEntity = new HttpEntity<MessageView>(messageView, headers);
        ResponseEntity<String> responseEntity = testRestTemplate.exchange(
                CONTEXT_ROOT + "/edit-message/" + messageEntityInput.getMessageId(), HttpMethod.POST, httpEntity,
                String.class);
        HttpStatus responseStatus = responseEntity.getStatusCode();
        assertEquals(HttpStatus.OK, responseStatus);
    }

1 个答案:

答案 0 :(得分:1)

No runnable methods at org.junit.runners.表示您的测试类没有任何@Test方法。

有关详细信息,请参阅此处:java.lang.Exception: No runnable methods exception in running JUnits