有没有办法用junit测试匿名内部类?

时间:2017-10-12 08:43:40

标签: java unit-testing testing anonymous-class

我有以下要测试的课程。必须测试一个匿名的内部类是非常困难的。任何帮助将不胜感激。

@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;
            }

        };
    }

}

1 个答案:

答案 0 :(得分:1)

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

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等的其他类。这将发现你的配置,这将导致你的Bean的实例化。
  3. 由于现在设置了应用程序上下文,我们可以像在普通应用程序代码中一样使用@Autowired注入bean。
  4. 我需要以下maven依赖项才能完成此任务(需要junit&gt; = 4.12)。所有这些都在maven中心提供:

        <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>