2016-07-24 69 views
0

我正在嘗試在我的applicatoin的其餘api上編寫測試。我正在使用彈簧靴,澤西島休息。對於測試,我試圖使用MockMvc,但對於所有GET請求,我獲得404狀態碼。我嘗試了不同的方式來運行它,但結果總是一樣的。我想我錯過了一些東西,所以,請指教。在測試spring boot rest api時未找到404

REST資源:

@Component 
@Path("/v1/users") 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
@Transactional(readOnly = true) 
public class UserResource { 
    private final UserService userService; 

    @Inject 
    public UserResource(UserService userService) { 
     this.userService = userService; 
    } 

    @GET 
    public Collection<User> list() { 
     return userService.fetchAllUsers(); 
    } 
} 

應用類:

@SpringBootApplication 
@ComponentScan("com.glasierr.application") 
@EnableJpaRepositories("com.glasierr.application.infrastructure.persistence.spring") 
public class UserServiceApplication { 
    public static void main(String[] args) { 
     new SpringApplicationBuilder() 
       .sources(UserServiceApplication.class) 
       .run(args); 
    } 
} 

新澤西配置:

@Configuration 
@ApplicationPath("/api") 
public class JerseyConfig extends ResourceConfig { 
    public JerseyConfig() { 
     packages("com.glasierr.application.resource"); 
    } 
} 

試驗性片段:

server: 
    port: 9999 

database: 
    driverName: org.h2.Driver 
    url: jdbc:h2:mem:test;IGNORECASE=TRUE;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1 
    username: sa 
    password: 
    hibernateDialect: org.hibernate.dialect.H2Dialect 
    hibernateShowSql: true 
    hibernateHbm2ddl: update 

測試類:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(UserServiceApplication.class) 
@WebIntegrationTest 
public class UserResourceTest { 

    @InjectMocks 
    private UserResource userResource; 

    @Mock 
    private UserService userService; 

    @Autowired 
    private WebApplicationContext context; 

    private MockMvc mvc; 

    @Before 
    public void setUp() { 
     MockitoAnnotations.initMocks(this); 

     mvc = MockMvcBuilders 
       .webAppContextSetup(context) 
       .build(); 
    } 

    @Test 
    public void test() throws Exception { 
     when(userService.fetchAllUsers()).thenReturn(Arrays.asList(new User("user1"), new User("user2"))); 

     mvc.perform(get("/api/v1/users").accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) 
       .andExpect(status().isOk()); 
    } 
} 

一些控制檯輸出:

2016-07-24 20:17:40.909 INFO 5532 --- [   main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 
2016-07-24 20:17:42.121 INFO 5532 --- [   main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]11c9af63: startup date [Sun Jul 24 20:17:32 EEST 2016]; root of context hierarchy 
2016-07-24 20:17:42.264 INFO 5532 --- [   main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 
2016-07-24 20:17:42.266 INFO 5532 --- [   main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 
2016-07-24 20:17:42.320 INFO 5532 --- [   main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2016-07-24 20:17:42.320 INFO 5532 --- [   main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2016-07-24 20:17:42.413 INFO 5532 --- [   main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 
2016-07-24 20:17:42.877 INFO 5532 --- [   main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9999 (http) 
2016-07-24 20:17:42.885 INFO 5532 --- [   main] e.s.h.a.resource.OperatorResourceTest : Started OperatorResourceTest in 10.898 seconds (JVM running for 13.017) 
2016-07-24 20:17:43.126 INFO 5532 --- [   main] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring FrameworkServlet '' 
2016-07-24 20:17:43.127 INFO 5532 --- [   main] o.s.t.web.servlet.TestDispatcherServlet : FrameworkServlet '': initialization started 
2016-07-24 20:17:43.148 INFO 5532 --- [   main] o.s.t.web.servlet.TestDispatcherServlet : FrameworkServlet '': initialization completed in 21 ms 

java.lang.AssertionError: Status 
Expected :200 
Actual :404 
+0

嘗試在測試類中加載JerseyConfig。您只加載UserServiceApplication('@SpringApplicationConfiguration(UserServiceApplication.class)'),並且JerseyConfig添加'api'路徑 –

回答

-1

問題解決了。我已經在應用程序類中包含了配置,並且它工作。

@SpringBootApplication 
@Configuration 
@Import({JerseyConfig.class}) 
public class UserServiceApplication { 
    public static void main(String[] args) { 
     new SpringApplicationBuilder() 
       .sources(UserServiceApplication.class) 
       .run(args); 
    }  
}