英文:
MockMvc test not reach the Controller
问题
我已经尝试了很多次,但很遗憾都没有成功。我不明白为什么我无法访问我的控制器。我必须通过独立设置来运行这个测试,因为我没有一个SpringBoot项目。
这是我的测试类:
@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);
}
运行测试后,我得到了这个答案:
[![enter image description here][1]][1]
当我进入Debug模式时,我可以看到mockMvc已经初始化,但是我也在我的控制器中设置了一个断点,但我无法达到这一点。
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/nrzEt.png
[2]: https://i.stack.imgur.com/Zk1iU.png
英文:
I've tried a lot but unfortunately without success. I don't understand why I can't reach my controller.I have to run this test via the standalone setup because I don't have a SpringBoot project.
This is my test 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());
}
}
This is my API that I'm trying to reach:
@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);
}
Running the Test I get this answer:
When i go into the Debug-Mode I can see that the mockMvc is initialized, but I have also set a debug point in my controller, but this I can´t reach.
答案1
得分: 1
@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>
英文:
@RunWith(SpringRunner.class)
@WebMvcTest(HelpPageController.class)
public class HelpPageControllerTest {
@Autowired
private MockMvc mockMvc;
...
}
or
@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
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
答案2
得分: 0
我得到了解决方案。我需要一个测试上下文配置。
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>
现在我还在模拟我的Service。这是我需要的代码:
@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());
}
}
英文:
I got the solution. I need a test Context Configuration.
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>
Now I am also mocking my Service. This is the code I need:
@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());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论