0
내 응용 프로그램의 나머지 API에 테스트를 쓰려고합니다. 나는 봄 부팅, 저지 휴식을 사용하고 있습니다. 테스트를 위해 MockMvc를 사용하려하지만 모든 GET 요청에 대해 404 상태 코드를 얻습니다. 나는 그것을 달리기 위해 여러 가지 방법을 시도했지만 결과는 항상 같습니다. 나는 뭔가를 놓치고 있다고 생각합니다. 제발, 조언하십시오.봄 부팅 나머지 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();
}
}
Application 클래스 :
@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
,451,515,
테스트 클래스 :
@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
테스트 클래스에서 JerseyConfig를로드하십시오. UserServiceApplication 만로드하고 ('@SpringApplicationConfiguration (UserServiceApplication.class)') JerseyConfig는'api' 경로를 추가합니다 –