我有一个关于使用SpringJUnit4ClassRunner进行测试的问题。

这是代码的缩写版本。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= "/application-config-test.xml")
@WebAppConfiguration
public class TestControllerTest {

    @Mock
    private TestService testService;

    @Autowired
    TestDao TestDao;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).build();
    }

       @Test
    public void checkServerName() throws Exception{
        TestServer testServer = new TestServer.TestServerBuilder()
                .withHostName("test1")
                .build();

        when(testService.selectTestServer("test1")).thenReturn(testServer);
        mockMvc.perform(get("/restapi/test/checkServerName")
                        .param("serverName", "test1"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(model().size(1))
                .andExpect(model().attributeExists())
                .andExpect(flash().attribute("message", "success"));
    }
}


这是config-test.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:security="http://www.springframework.org/schema/security"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.directwebremoting.org/schema/spring-dwr
           http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd
           http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
           http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.server.test"/>
    <context:component-scan base-package="com.server.db.mybatis"/>
</beans>


这是控制器逻辑

@GetMapping(value="/checkServerName")
    public something checkServerName(@RequestParam (value="serverName")String serverName){
       TestServer testServer = testService.selectTestServer(serverName);
        if(edgeServer == null){
                ...
        }else{
                ...
        }
        return something;
    }



上面的测试失败了,首先是因为testService为null。
我想模拟一个服务,以便它可以响应特定的对象,即我制作的testServer。但是以某种方式失败了,我尝试了@MockBean @InjectMocks @Autowired,但是全部失败了。

我该如何嘲笑这项服务?并假设我已经成功地模拟了该服务bean,那么自动装配控制器可以使用该模拟的bean执行其逻辑吗?

最佳答案

如何在控制器中定义testService


如果将其定义为@Autowired private TestService testService,则需要设置模拟testService来代替它(更简单的方法-RelectionTestUtils.setField(controller, "testService", testService)
如果在控制器的构造函数中(这是一种正确的方法-Spring坚持这样),只需使用模拟testService在测试类中创建新的控制器。


之后,您可以控制此testService并测试您的逻辑。

10-06 14:05