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

标签 java unit-testing testing anonymous-class

我想测试以下类(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;
            }

        };
    }

}

最佳答案

这应该是测试内部类所需的最低限度:

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 注入(inject) 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>

关于java - 有什么方法可以使用 junit 测试匿名内部类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46705119/

相关文章:

java - LuaJava编译错误 "Unresolved external symbol"

java - 创建一个函数来检查数组中的任何索引是否具有相同的值

c# - 为什么我的某些单元测试会运行多次?

c# - 使用 Moq 验证调用的顺序是否正确

ruby - 在 Ruby 中创建用于单元测试的虚拟服务器

java - 使用邻居递增地计算矩阵中的元素

java - <identifier> 地址簿比较器出现预期错误

c# - 如何使用 moq 和 mspec(BDD 风格)比较两个对象列表

使用安全帽通过 Uniswap 流动性配置测试代币

Android 测试 : The storage state is mounted, WRITE_EXTERNAL_STORAGE 权限已声明,但仍然无法写入模拟器的 sdcard