mercredi 4 décembre 2019

Testing Controller logic that contains service with SpringJUnit4ClassRunner

I have a question regarding testing with SpringJUnit4ClassRunner.

Here is a abbreviated version of code.

@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"));
    }
}

And here is the config-test.xml file.

<?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>

Here is controller logic

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

The above test fails, firstly because the testService is null. I want to mock a service so that it answers with a specific object, namely the testServer I made. But somehow it fails, and I've tried @MockBean @InjectMocks @Autowired but it all fails.

How can I mock this service? and suppose that I have successfully mocked this service bean, then can the autowired controller use this mocked bean for executing its logic?

Aucun commentaire:

Enregistrer un commentaire