2 回答

TA貢獻1811條經(jīng)驗 獲得超5個贊
好的,謝謝大家的幫助。無意沮喪,發(fā)布配置和流程對我的想法沒有幫助,因為我在下面發(fā)現(xiàn)了:
仔細檢查有一個例外:
org.springframework.expression.AccessException: Could not resolve bean reference against BeanFactory
有問題的參考是對我使用 @SpyBean 的實用程序中的一個方法:
<int:transformer
expression="@utilities.asMap('licence_id', headers[licenceId], 'message', 'Delivered: ' + headers[confirmedMessage], 'secured_session_id', headers[visitorSession].getSecureSessionId())" />
它不是一個單獨的 ApplicationContext 而是 SpEL 不會接受間諜 bean,因為引用已更改或類似。
所以,我不理會這些實用程序,而是改造了它內(nèi)部的另一個 bean 來生成數(shù)字,并在其上使用了 SpyBean?,F(xiàn)在 Spring Integration/SpEL 再次感到高興,因為它使用的實用程序 bean 是正確的,并且模擬發(fā)生在該 bean 內(nèi)部并且對 SpEL 透明。
@Component
public class RandomSupplier implements Supplier<Double> {
@Override
public Double get() {
return Math.random();
}
}
public class FullIntegrationTest {
@Autowired
private MockMvc mvc;
@SpyBean
private RandomSupplier randomSupplier;
@Autowired // This is only necessary for the toy test below
private Utilities utilities;
@BeforeEach
public void setupAfterInit() {
Mockito.when(randomSupplier.get()).thenReturn(0.5);
}
@Test
public void t0() throws IOException {
System.out.println(utilities.getRandom());
}
...
現(xiàn)在 Spring Integration/SpEL 再次感到高興,因為它工作的實用程序 bean 是正確的,并且模擬發(fā)生在該 bean 內(nèi)部。
三個教訓(xùn):不要窺探 Spring Integration Flow 中 SpEL 中直接引用的 bean;閱讀日志;你永遠不可能有足夠的間接性:)

TA貢獻1864條經(jīng)驗 獲得超6個贊
我試圖用最少的配置重現(xiàn)你的問題:
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {Ctx.class})
public class XTest {
@SpyBean
private Random random1;
@Autowired private Supplier<Integer> intSupplier;
@Test
public void test() {
Mockito.when(random1.nextInt()).thenReturn(Integer.MAX_VALUE);
int i = intSupplier.get();
System.out.println("i=" + i);
}
@Configuration
public static class Ctx {
@Bean
static Random random1() {
return ThreadLocalRandom.current();
}
@Bean
static Supplier<Integer> intSupplier(Random random1) {
return random1::nextInt;
}
}
}
正如預(yù)期的那樣打印
i=2147483647
所以,你的運行時配置一定有問題......你能分享一下嗎?我猜 spring-integration 正在使用另一個 ApplicationContext。我知道這不是答案,如果沒有幫助,我會刪除它。
添加回答
舉報