MockMvc测试未能到达控制器。

huangapple go评论78阅读模式
英文:

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(&quot;/help/manuals?lang=de&quot;));
        resultActions.andExpect(status().isOk());
    }
}

This is my API that I'm trying to reach:

      @GetMapping(&quot;/help/manuals&quot;)
      public ResponseEntity&lt;List&lt;ManualResponseTO&gt;&gt; getManuals(@RequestParam String lang) {
       List&lt;ManualResponseTO&gt; manuals;
       manuals = this.helpService.getManuals(lang);
       return new ResponseEntity&lt;&gt;(manuals, HttpStatus.OK);
     }

Running the Test I get this answer:

MockMvc测试未能到达控制器。

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.

MockMvc测试未能到达控制器。

答案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

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;

答案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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

&lt;/beans&gt;

Now I am also mocking my Service. This is the code I need:

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = &quot;classpath*:testContext.xml&quot;)
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&lt;ManualResponseTO&gt; manualResponseTOS = new ArrayList&lt;&gt;();
        when(helpService.getManuals(&quot;de&quot;)).thenReturn(manualResponseTOS);
        ResultActions resultActions = mockMvc.perform(get(&quot;/manuals?lang=de&quot;));
        resultActions.andExpect(status().isOk());
    }
}

huangapple
  • 本文由 发表于 2020年10月5日 21:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64209949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定