Java 莫基托+;放心不要在Spring Boot Rest MVC应用程序中模拟(集成测试)

Java 莫基托+;放心不要在Spring Boot Rest MVC应用程序中模拟(集成测试),java,spring,junit,spring-boot,mockito,Java,Spring,Junit,Spring Boot,Mockito,我试图为SpringBoot MVC rest应用程序编写测试用例。我能够成功地运行测试用例。但当我试图模仿其中一种方法时,它却不起作用。 测试用例仍然调用原始实现 测试等级:- @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = { Initializer.class, WebConfig.class }) @WebAppConfiguration public c

我试图为SpringBoot MVC rest应用程序编写测试用例。我能够成功地运行测试用例。但当我试图模仿其中一种方法时,它却不起作用。 测试用例仍然调用原始实现

测试等级:-

 @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();

    }
}
预处理器是一个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>

org.springframework.boot
弹簧起动试验
测试
朱尼特
朱尼特
4.11
测试
com.fasterxml.jackson.core
杰克逊核心
2.5.3
com.fasterxml.jackson.core
杰克逊数据绑定
2.5.3
org.mockito
莫基托磁芯
1.9.0
测试
org.hamcrest
汉克雷斯特岩芯
1.3
测试
com.jayway.restassed
spring模拟mvc
2.4.1
测试
没有得到任何错误。执行工作正常,但不考虑模拟实现的性能


欢迎提出任何建议或建议。

我终于发现它起作用了!!! 请在下面找到已更改的测试类。使用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();

    }
}

我怀疑这与testrunner@RunWith(SpringJUnit4ClassRunner.class)有关,谢谢你的回复。作为用于集成测试的SpringMVC项目,我不能给任何其他测试运行程序。也许可以尝试MockitoAnnotations。initMocks(此)谢谢。initMocks(this)被添加到@Before方法中。