第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

SpyBean 沒有被注入到任何地方

SpyBean 沒有被注入到任何地方

不負相思意 2021-09-03 14:35:39
我很難將 spy bean 放入我的 ApplicationContext。我有一顆豆稱為公用事業(yè)類型的工具:@Component("utilities")public class Utilities {<snip>    /**     * Returns a random int. This is provided mostly for testing mock-ability     *     * @return a random integer     */    public int getRandom() {        return (int) (Math.random() * Integer.MAX_VALUE);    }}它是在我的 Spring 集成流程間接引用的類中使用的。然后我有這個木星測試:@TestInstance(Lifecycle.PER_CLASS)@FixMethodOrder(MethodSorters.NAME_ASCENDING)@ExtendWith(SpringExtension.class)@ContextConfiguration( classes = {    XmlLocations.class,    VisitorManager.class,    Utilities.class,    UnixTimeChannel.class})@WebMvcTest//@TestExecutionListeners( { MockitoTestExecutionListener.class })public class FullIntegrationTest {    @Autowired    private MockMvc mvc;    @SpyBean    private Utilities utilities;    private ClientAndServer mockServer;    private static final int MOCK_SERVER_PORT = 9089;    @BeforeAll    public void setUpBeforeClass() {        Mockito.when(utilities.getRandom()).thenReturn(Integer.MAX_VALUE);        mockServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);        RestAssuredMockMvc.mockMvc(mvc);        (new MockServerPingInit()).initializeExpectations(mockServer);        (new MockServerFullIntegrationInit()).initializeExpectations(mockServer);    }    @Test    public void t00200_IncomingMessage() {        RestAssuredMockMvc.given()            .queryParam("example", "example")            .when()            .request("POST", "/api/v1/incoming")            .then()            .statusCode(equalTo(200));    }<snip>但是即使我創(chuàng)建了 spy bean 并在它上面使用了 when/thenReturn,它也不會漂浮到我的應(yīng)用程序上下文中等待被調(diào)用并返回它的模擬隨機值。我知道utilities.getRandom() 方法被調(diào)用,因為我可以在它上面放置一個斷點并調(diào)試測試,并且它命中了getRandom 方法,但是當我嘗試添加一個如上所示的間諜bean 并模擬getRandom 時返回一個固定值來測試斷點仍然命中,所以我可以告訴真正的方法不是正在調(diào)用模擬。我試過將 when/thenReturn 也放在測試中,以防為時過早,但這無濟于事。顯然我做錯了什么,可能在概念上是錯誤的。呸!
查看完整描述

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;閱讀日志;你永遠不可能有足夠的間接性:)


查看完整回答
反對 回復(fù) 2021-09-03
?
慕尼黑的夜晚無繁華

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。我知道這不是答案,如果沒有幫助,我會刪除它。


查看完整回答
反對 回復(fù) 2021-09-03
  • 2 回答
  • 0 關(guān)注
  • 507 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號