0

spring mvc コントローラーでテストを実行しようとしていますが、applicationContext は常に null です。

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.web.WebAppConfiguration
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.ResultActions
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import spock.unitils.UnitilsSupport

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@UnitilsSupport
@WebAppConfiguration
//@ContextConfiguration(classes=Config.class)
//@ContextConfiguration(loader= ApplicationContextProvider.class)
class TimeSheetControllerIt extends TestsSupport {

    @Autowired
    private WebApplicationContext webApplicationContext; //NULL

    //private MockHttpServletRequest mockHttpServletRequest;
    private MockMvc mockMvc
    private ResultActions resultActions

    def setup(){
        //this.mockHttpServletRequest = new MockHttpServletRequest();
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

どうすればいいですか?

4

2 に答える 2

0

コンテキスト構成の場所が指定されていません。そのため、Spring はファイルの場所ではなく、そこから Bean を作成し、必要な場所に注入できます。

メイン アプリケーションとテスト用のコンテキスト構成の場所が異なることに注意してください。したがって、web.xml で指定したとしても、スコープにはテストが含まれません。テスト クラスで明示的に指定する必要があります。

于 2013-05-22T05:23:35.820 に答える
0

コンテキスト構成 (コメントした @ContextConfiguration) を提供してみてください。

ログをチェックして、アプリケーション コンテキストが適切に初期化されているかどうかを確認します。

于 2013-05-21T11:26:24.903 に答える