37 lines
2.1 KiB
Java
37 lines
2.1 KiB
Java
|
|
package com.common.service;
|
||
|
|
|
||
|
|
import com.config.CacheConfig;
|
||
|
|
import com.config.WebConfig;
|
||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
|
||
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||
|
|
import org.springframework.mock.env.MockEnvironment;
|
||
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
||
|
|
|
||
|
|
import static org.junit.jupiter.api.Assertions.*;
|
||
|
|
|
||
|
|
/** 使用实际配置创建两个同类型Bean,覆盖现场启动时的构造器自动装配。 */
|
||
|
|
class StatsProjectionPublisherWiringTest {
|
||
|
|
@Test
|
||
|
|
void startsWithCacheAndHttpMappersWithoutChangingSharedSerialization() {
|
||
|
|
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
||
|
|
context.setEnvironment(new MockEnvironment()
|
||
|
|
.withProperty("stats.projection-async.topic", "test.STATS.PROJECTION"));
|
||
|
|
context.registerBean("objectMapper", ObjectMapper.class, () -> new CacheConfig().objectMapper());
|
||
|
|
context.registerBean("httpObjectMapper", ObjectMapper.class, () -> new WebConfig().httpObjectMapper());
|
||
|
|
context.registerBean(KafkaProperties.class);
|
||
|
|
context.register(StatsProjectionAsyncPublisher.class);
|
||
|
|
assertDoesNotThrow(context::refresh);
|
||
|
|
assertEquals(2, context.getBeansOfType(ObjectMapper.class).size());
|
||
|
|
StatsProjectionAsyncPublisher publisher = context.getBean(StatsProjectionAsyncPublisher.class);
|
||
|
|
ObjectMapper shared = context.getBean("objectMapper", ObjectMapper.class);
|
||
|
|
ObjectMapper copied = (ObjectMapper) ReflectionTestUtils.getField(publisher, "mapper");
|
||
|
|
assertNotSame(shared, copied);
|
||
|
|
assertEquals(shared.getRegisteredModuleIds(), copied.getRegisteredModuleIds());
|
||
|
|
assertNotNull(shared.getSerializationConfig().getDefaultTyper(shared.constructType(Object.class)));
|
||
|
|
assertNull(copied.getSerializationConfig().getDefaultTyper(copied.constructType(Object.class)));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|