繁体   English   中英

有什么方法可以使用junit测试匿名内部类吗?

[英]Is there any way to test an anonymous inner class using junit?

我有以下课程想测试。 事实证明,必须测试匿名内部类非常困难。 任何帮助,将不胜感激。

@Configuration
public class CustomErrorConfiguration {

    @Bean
    public ErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes() {

            @Override
            public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
                    boolean includeStackTrace) {
                Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
                errorAttributes.remove("timestamp");
                errorAttributes.remove("status");
                errorAttributes.remove("exception");
                errorAttributes.remove("path");
                if (errorAttributes.containsKey("error") && errorAttributes.containsKey("message")) {
                    Map<String, Object> attr = new HashMap<>();
                    attr.put("message", errorAttributes.get("message"));
                    return attr;
                }
                return errorAttributes;
            }

        };
    }

}

这应该是测试内部类所需的最低要求:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration
public class BeanTest {

    @Autowired
    ErrorAttributes errorAttributes;

    @Test
    public void testMyBean() {
        RequestAttributes requestAttributes = new RequestAttributes();
        System.out.println(errorAttributes.getErrorAttributes(requestAttributes, true));
    }

    @Configuration
    @ComponentScan(basePackages = "package.where.your.configuration.is")
    public static class SpringTestConfig {

    }
}

该测试执行以下操作:

  1. 它利用SpringRunner.class为您创建一个ApplicationContext进行测试。
  2. @ContextConfiguration批注拾取静态嵌套类SpringTestConfig。 此类很繁重,实际上会扫描基本包以查找标记有Configuration,Bean,Component等的其他类。这将发现您的Configuration,进而导致实例化Bean。
  3. 因为现在已经设置了应用程序上下文,所以我们可以像在普通应用程序代码中一样使用@Autowired注入bean。

我需要以下Maven依赖项才能完成此操作(需要Junit> = 4.12)。 全部在Maven Central中可用:

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.0.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM