小编Ser*_*kin的帖子

如何在测试之间关闭应用程序上下文

我有 3 组测试:单元、集成、验收。

  • 后两组启动 ApplicationContext:“集成”最少,“接受”完整。
  • 两个应用程序上下文都注册队列订阅者。
  • 在整个测试运行结束时注销应用程序上下文 ( @RunWith(SpringRunner.class))

当我运行“所有测试”时,会启动 2 个不同的应用程序上下文,并且我有重复的队列订阅者。

我知道此订阅者重复的以下解决方法:

  • 永远不要同时运行集成和验收测试
  • 将“验收”应用程序上下文用于“集成”测试。缺点:试运行需要更长的时间。
  • 添加静态注册表并手动添加/删除侦听器。缺点:过于复杂且容易忘记

在一组测试之后,是否有任何方便的方法来卸载应用程序上下文?

基于ndrone答案的更新

  • @DirtiesContext 是绝配
  • 另一种选择是将缓存的 ApplicationContexts 计数限制为 1 spring.test.context.cache.maxSize=1

使用脏上下文测试超类示例

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
    @TestExecutionListeners({FlywayTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
    public abstract class AcceptanceTest {}
Run Code Online (Sandbox Code Playgroud)

applicationcontext spring-boot spring-boot-test

5
推荐指数
0
解决办法
2550
查看次数

为什么java的HashMap会重新检查存储桶中的哈希码

当HashMap搜索密钥时,它在2个位置使用密钥的哈希码:

  1. 选择桶
  2. 在bucket中查找条目(openjdk7 HashMap get方法源码)

    
        public V get(Object key) {
            if (key == null)
                return getForNullKey();
            int hash = hash(key.hashCode());
            for (Entry e = table[indexFor(hash, table.length)];
                 e != null;
                 e = e.next) {
                Object k;
                if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
                    return e.value;
            }
            return null;
        }
    
    Run Code Online (Sandbox Code Playgroud)

为什么HashMap正在检查桶内的哈希码?为什么仅仅检查存储桶内的引用和对象是否足够?

java hashmap

1
推荐指数
1
解决办法
62
查看次数