gpt4 book ai didi

java - 使用 Spring Boot Test 进行错误测试

转载 作者:搜寻专家 更新时间:2023-11-01 03:47:25 28 4
gpt4 key购买 nike

我正在尝试对我的服务实现简单的单元测试。我的测试类代码如下:

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {

@Mock
private UserRepository userRepoMock;
private UserService userService;
private List<User> users;

@Before
public void setUp() throws Exception {
userService = new UserService(userRepoMock);

User u1 = new User();
u1.setEmail("test@test.com");
User u2 = new User();
u2.setEmail("test2@test.com");
users = Arrays.asList(u1, u2);
}

@Test
public void getAll() throws Exception {
when(userRepoMock.findAll()).thenReturn(users);

List<UserDTO> all = userService.getAll(new PageRequest(0, 20));
verify(userRepoMock).findAll();
}
}

执行测试时,我总是遇到异常,这是摘录:

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoClassDefFoundError: net/minidev/json/writer/JsonReaderI
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is java.lang.NoClassDefFoundError: net/minidev/json/writer/JsonReaderI
...
Caused by: java.lang.NoClassDefFoundError: net/minidev/json/writer/JsonReaderI
...
Caused by: java.lang.ClassNotFoundException: net.minidev.json.writer.JsonReaderI
...

我的代码有什么问题?我是否应该包含一些 spring-boot-starter-test 中未包含的依赖项?

注意我使用的是 Spring Boot v 1.5.1 和 Spring Data MongoDB 存储库。

最佳答案

你使用json-smart-v2吗?

在那里你可以看到你的异常中的类存在: Github link

您可能有错误的版本或一些不正确的依赖项。

关于java - 使用 Spring Boot Test 进行错误测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42667081/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com