MockMvc在控制器位于不同包中时不起作用(JUnit5)

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

MockMvc Not working when Controller is in different package (JUnit5)

问题

package com.azry.ptm.api;

import com.azry.ptm.api.model.account.AccountDTO;
import com.azry.ptm.domain.account.Account;
import com.azry.ptm.server.services.AccountService;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.anyLong;

@AutoConfigureMockMvc
@SpringBootTest
public class AccountControllerImplTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    public AccountService accountService;

    @Test
    public void test() throws Exception {

        final long entityNo = 10;
        Account expectedAccount = Account.builder()
                .entityNo(entityNo)
                .build();
        Mockito.when(accountService.getAccountById(anyLong())).thenReturn(Optional.of(expectedAccount));

        MockHttpServletResponse response = mockMvc.perform(ControllerTestHelper.makeGetRequest("account/", String.valueOf(entityNo)))
                .andReturn()
                .getResponse();
        AccountDTO responseAccount = ControllerTestHelper.toObject(response.getContentAsString(), AccountDTO.class);

        assertEquals(HttpStatus.OK.value(), response.getStatus());
        assertNotNull(responseAccount);
    }
}

这是您提供的代码翻译后的结果。如果您还有其他需要翻译的内容或问题,请随时提问。

英文:
package com.azry.ptm.api;
import com.azry.ptm.api.model.account.AccountDTO;
import com.azry.ptm.domain.account.Account;
import com.azry.ptm.server.services.AccountService;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.anyLong;
@AutoConfigureMockMvc
@SpringBootTest
public class AccountControllerImplTest {
@Autowired
private MockMvc mockMvc;
@MockBean
public AccountService accountService;
@Test
public void test() throws Exception {
final long entityNo = 10;
Account expectedAccount = Account.builder()
.entityNo(entityNo)
.build();
Mockito.when(accountService.getAccountById(anyLong())).thenReturn(Optional.of(expectedAccount));
MockHttpServletResponse response = mockMvc.perform(ControllerTestHelper.makeGetRequest("account/", String.valueOf(entityNo)))
.andReturn()
.getResponse();
AccountDTO responseAccount = ControllerTestHelper.toObject(response.getContentAsString(), AccountDTO.class);
assertEquals(HttpStatus.OK.value(), response.getStatus());
assertNotNull(responseAccount);
}

}

Here is my mockMvc test. it works only when the controller is in the same module es test, otherwise, when I split the project it returns a 404 error code as no endpoint was found.

has anybody experience using mockMvc in a multi-module spring-boot app?

答案1

得分: 1

使用 @WebMvcTest 注解解决

    @WebMvcTest(AccountControllerImpl.class)
英文:

solved using @WebMvcTest annotation

@WebMvcTest(AccountControllerImpl.class)

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

发表评论

匿名网友

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

确定