2017-03-08 39 views
2

我正在嘗試對我的服務執行簡單的單元測試。我的測試類的代碼如下:使用Spring Boot Test進行錯誤測試

@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("[email protected]"); 
     User u2 = new User(); 
     u2.setEmail("[email protected]"); 
     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存儲庫。

+0

您的應用程序是否正常啓動或您是否也收到了這些相同的錯誤?使用Maven或Gradle進行依賴? –

+0

@WimDeblauwe我正在使用Maven,是的,我的應用程序開始正確... – davioooh

+0

您可能想要使用Maven'dependency:tree'命令來查看是否包含所需的依賴項。請參閱https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html您是從Maven還是從IDE運行測試?如果從你的IDE,你確保最新的pom.xml更改被導入? –

回答

0

你使用json-smart-v2

在那裏,你可以看到你的異常類存在: Github link

你有可能是錯誤的版本,或者一些不正確的依賴關係。

相關問題