spring WebMvcTest和实体管理器工厂缺少引用

jgwigjjp  于 2023-03-22  发布在  Spring
关注(0)|答案(1)|浏览(146)

我用 Spring Boot 3
我尝试从控制器进行测试

userManager.findByUa

调用存储库
试验

@ExtendWith(SpringExtension.class)
@WebMvcTest(value = UserController.class)
public class UserConnectionControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserManager userManager;

    @Test
    public void retrieveUserConnection() throws Exception{

        UserConnection mockUserConnection = new UserConnection(158791, "Paul Smith", "1123977", "01");

mockUserConnection.setDisabled(false);

        String expected = """
        {
            "userId": 158791,
                "name": "Paul Smith",
                "userAccess": "1123977",
                "userType": "01",
                "disabled": false
        }
        """;

        Mockito.when(userManager.findByUa(Mockito.anyString())).thenReturn(mockUserConnection);

        RequestBuilder requestBuilder = MockMvcRequestBuilders.get(
                "/users/1123977").accept(
                MediaType.APPLICATION_JSON);

        MvcResult result = mockMvc.perform(requestBuilder).andReturn();

        JSONAssert.assertEquals(expected, result.getResponse()
                .getContentAsString(), false);

    }

}

当我运行这个测试时,我得到这个错误
org.springframework.beans.factory.BeanCreationException:创建名为“jpaSharedEM_entityManagerFactory”的Bean时出错:设置构造函数参数时无法解析对bean“entityManagerFactory”的引用
主类

@SpringBootApplication(
        scanBasePackages = { "com.acme.extranet", "com.acme.common" }
)
@EntityScan("com.acme.common")
@EnableJpaRepositories(basePackages = "com.acme.common")
public class ExtranetApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExtranetApplication.class, args);
    }

}

我的实体和存储库以及通用包中的jar库在Extranet应用中使用

j9per5c4

j9per5c41#

我不知道你的背景和测试策略,所以这不是一个准确的提示。我认为有两个选择。

1.没有@MockBean

如果你只是想使用UserManager作为测试双精度,只需通过Mockito的静态方法制作mock对象。删除JPA的依赖项进行测试。

package action.in.blog;

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.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;

@WebMvcTest(value = UserController.class)
public class TempTest {

    @Autowired
    private MockMvc mockMvc;

    private UserManager userManager;

    @Test
    void test() {
        userManager = Mockito.mock(UserManager.class);
        when(userManager.findAll()).thenReturn(
                List.of(new User(111))
        );

        List<User> result = userManager.findAll();

        assertThat(mockMvc != null, equalTo(true));
        assertThat(result.get(0).getId(), equalTo(111L));
    }
}

2.使用@SpringBooTest

@WebMvcTest annotation为MVC测试提供了最小的上下文。因此无法使用JPA功能。并且您需要附加两个annotation。

  • @AutoConfigureMockMvc用于MockMvc注入。
  • @Transational用于测试后回滚事务。
package action.in.blog;

import org.junit.jupiter.api.Test;
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.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;

@AutoConfigureMockMvc
@Transactional
@SpringBootTest
public class TempSecondTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserManager userManager;

    @Test
    void test() {
        when(userManager.findAll()).thenReturn(
                List.of(new User(111))
        );

        List<User> result = userManager.findAll();

        assertThat(mockMvc != null, equalTo(true));
        assertThat(result.get(0).getId(), equalTo(111L));
    }
}

相关问题