簡體   English   中英

Mockito + Rest-Assured在Spring Boot REST MVC應用程序中未進行模擬(集成測試)

[英]Mockito+ Rest-Assured not mocking in Spring Boot REST MVC application(Integration Testing)

我試圖為SpringBoot MVC其余應用程序編寫測試用例。 我能夠成功運行測試用例。 但是,當我嘗試模擬其中一種方法時,它不起作用。 測試用例仍然調用原始實現。

測試類別:-

 @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { Initializer.class,
        WebConfig.class })
@WebAppConfiguration
public class SampleControllerTest {

    @Mock
    CounterUtil counterUtil;

    @InjectMocks
    PreProcessor preProcessor

    @Autowired
    private WebApplicationContext context;
    private static boolean isSetup = false;
    @Test
    public void sampleTest() {              
        org.mockito.Mockito.when(
                counterUtil
                        .getCounter()).thenReturn(
                "2222");
        given().contentType("application/json").when()
        .get("/initialize").then()
        .statusCode(HttpServletResponse.SC_OK);
    }
    @Before
    public void setUp() {
        RestAssured.useRelaxedHTTPSValidation();

            counterUtil = (CounterUtil) context
                    .getBean("counterUtil");    
            MockitoAnnotations.initMocks(this);
             RestAssuredMockMvc.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();

    }
}

PreProcessor是一個concreate類,帶有CounterUtil的實例。

@Component
public class PreProcessor {

    @Autowired
    private CounterUtil counterUtil


    public void myMethod(){
        counterUtil.getCounter();
    }

}

這些是pom.xml中的依賴項。

<!-- test dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>1.9.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>spring-mock-mvc</artifactId>
            <version>2.4.1</version>
            <scope>test</scope>
        </dependency>

沒有收到任何錯誤。 執行工作正常,但不考慮模擬的實現。

也歡迎任何建議或指示。

我終於出來了!!! 請在下面的測試類中找到已更改的內容。 使用codes.org.springframework.test.util.ReflectionTestUtils類注入​​模擬對象。

 @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { Initializer.class,
        WebConfig.class })
@WebIntegrationTest
public class SampleControllerTest {

    @Autowired
    CounterUtil counterUtil;

    @Autowired
    PreProcessor preProcessor

    @Autowired
    private WebApplicationContext context;
    private static boolean isSetup = false;
    @Test
    public void sampleTest() {              
        org.mockito.Mockito.when(
                counterUtil
                        .getCounter()).thenReturn(
                "2222");
        given().contentType("application/json").when()
        .get("/initialize").then()
        .statusCode(HttpServletResponse.SC_OK);
    }
    @Before
    public void setUp() {
            counterUtil = (CounterUtil) context
                    .getBean("counterUtil");   
            counterUtil = Mockito
                    .mock(CounterUtil.class);
            ReflectionTestUtils.setField(preProcessor,
                    "counterUtil", counterUtil);                    
            MockitoAnnotations.initMocks(this);
             RestAssuredMockMvc.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM