MockMvc 测试未到达控制器
MockMvc test not reach the Controller
我尝试了很多,但不幸的是没有成功。我不明白为什么我无法通过独立设置 controller.I 必须 运行 此测试,因为我没有 SpringBoot 项目。
这是我的测试class:
@RunWith(SpringJUnit4ClassRunner.class)
public class HelpPageControllerTest {
@Mock
private HelpService helpService;
private MockMvc mockMvc;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders
.standaloneSetup(new HelpPageController())
.build();
}
@Test
public void justATest() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/help/manuals?lang=de"));
resultActions.andExpect(status().isOk());
}
}
这是我要达到的 API:
@GetMapping("/help/manuals")
public ResponseEntity<List<ManualResponseTO>> getManuals(@RequestParam String lang) {
List<ManualResponseTO> manuals;
manuals = this.helpService.getManuals(lang);
return new ResponseEntity<>(manuals, HttpStatus.OK);
}
运行 测试我得到这个答案:
当我进入调试模式时,我可以看到 mockMvc 已初始化,但我还在我的控制器中设置了一个调试点,但我无法到达。
@RunWith(SpringRunner.class)
@WebMvcTest(HelpPageController.class)
public class HelpPageControllerTest {
@Autowired
private MockMvc mockMvc;
...
}
或
@RunWith(SpringRunner.class)
@WebMvcTest(HelpPageController.class)
public class HelpPageControllerTest {
private MockMvc mockMvc;
@Autowired
private HelpPageController helpPageController;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders
.standaloneSetup(helpPageController)
.build();
}
...
}
依赖性
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我得到了解决方案。我需要测试 上下文配置。
testContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
现在我也在嘲笑我的服务。这是我需要的代码:
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = "classpath*:testContext.xml")
public class HelpPageControllerTest {
private MockMvc mockMvc;
@Mock
private HelpService helpService;
@InjectMocks
private HelpPageController helpPageController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders
.standaloneSetup(helpPageController)
.build();
}
@Test
public void justATest() throws Exception {
List<ManualResponseTO> manualResponseTOS = new ArrayList<>();
when(helpService.getManuals("de")).thenReturn(manualResponseTOS);
ResultActions resultActions = mockMvc.perform(get("/manuals?lang=de"));
resultActions.andExpect(status().isOk());
}
}
我尝试了很多,但不幸的是没有成功。我不明白为什么我无法通过独立设置 controller.I 必须 运行 此测试,因为我没有 SpringBoot 项目。
这是我的测试class:
@RunWith(SpringJUnit4ClassRunner.class)
public class HelpPageControllerTest {
@Mock
private HelpService helpService;
private MockMvc mockMvc;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders
.standaloneSetup(new HelpPageController())
.build();
}
@Test
public void justATest() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/help/manuals?lang=de"));
resultActions.andExpect(status().isOk());
}
}
这是我要达到的 API:
@GetMapping("/help/manuals")
public ResponseEntity<List<ManualResponseTO>> getManuals(@RequestParam String lang) {
List<ManualResponseTO> manuals;
manuals = this.helpService.getManuals(lang);
return new ResponseEntity<>(manuals, HttpStatus.OK);
}
运行 测试我得到这个答案:
当我进入调试模式时,我可以看到 mockMvc 已初始化,但我还在我的控制器中设置了一个调试点,但我无法到达。
@RunWith(SpringRunner.class)
@WebMvcTest(HelpPageController.class)
public class HelpPageControllerTest {
@Autowired
private MockMvc mockMvc;
...
}
或
@RunWith(SpringRunner.class)
@WebMvcTest(HelpPageController.class)
public class HelpPageControllerTest {
private MockMvc mockMvc;
@Autowired
private HelpPageController helpPageController;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders
.standaloneSetup(helpPageController)
.build();
}
...
}
依赖性
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
我得到了解决方案。我需要测试 上下文配置。
testContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
现在我也在嘲笑我的服务。这是我需要的代码:
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = "classpath*:testContext.xml")
public class HelpPageControllerTest {
private MockMvc mockMvc;
@Mock
private HelpService helpService;
@InjectMocks
private HelpPageController helpPageController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders
.standaloneSetup(helpPageController)
.build();
}
@Test
public void justATest() throws Exception {
List<ManualResponseTO> manualResponseTOS = new ArrayList<>();
when(helpService.getManuals("de")).thenReturn(manualResponseTOS);
ResultActions resultActions = mockMvc.perform(get("/manuals?lang=de"));
resultActions.andExpect(status().isOk());
}
}