[英]Mockito+ Rest-Assured not mocking in Spring Boot REST MVC application(Integration Testing)
I was trying to write test cases for SpringBoot MVC rest application. 我试图为SpringBoot MVC其余应用程序编写测试用例。 I am able to run the test cases successfully. 我能够成功运行测试用例。 But when i tried to mock one of the methods, its not working. 但是,当我尝试模拟其中一种方法时,它不起作用。 The test case still calls the original implementation. 测试用例仍然调用原始实现。
Test class:- 测试类别:-
@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();
}
}
The PreProcessor is a concreate class, with an instance of CounterUtil. PreProcessor是一个concreate类,带有CounterUtil的实例。
@Component
public class PreProcessor {
@Autowired
private CounterUtil counterUtil
public void myMethod(){
counterUtil.getCounter();
}
}
These are the dependencies in pom.xml. 这些是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>
Not getting any error. 没有收到任何错误。 The execution works fine, but not considering the the mocked implementation. 执行工作正常,但不考虑模拟的实现。
Any suggestion or pointer is also welcomed. 也欢迎任何建议或指示。
I Finally out it working!!! 我终于出来了!!! Please find below the test class with the changed. 请在下面的测试类中找到已更改的内容。 Used codes.org.springframework.test.util.ReflectionTestUtils class for injesting the mock objects. 使用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.