当前位置: 首页 > 知识库问答 >
问题:

测试spring boot rest api时找不到404

马冯浩
2023-03-14
@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.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@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

共有1个答案

裘安阳
2023-03-14
@SpringBootApplication
@Configuration  
@Import({JerseyConfig.class})
public class UserServiceApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder()
                .sources(UserServiceApplication.class)
                .run(args);
    }    
}
 类似资料:
  • 运行后,它找不到我的ANDROID_SDK_ROOT 未定义$Android_SDK_ROOT,请将SDK安装目录的路径设置为$Android_SDK_ROOT, 它已经在我的/.zshrc文件中定义 导出Android_SDK=/users/chiwan/library/android/sdk导出路径=/users/chiwan/library/android/sdk/platform-tool

  • 我有一个Maven项目设置。在这个项目中,我使用JUnit进行单元测试。当我使用或运行测试时,不会运行任何测试。测试位于中,它们都以结束(它们匹配)。 这是我的pom.xml: 我的整个项目结构可以在这里看到:https://bitbucket.org/equator-lang/ppp4e/src/master/ 任何帮助都将非常感谢,因为我已经尝试了很多个小时来让这个工作,但不知道该做什么。 公

  • 我在测试一个android项目时遇到了麻烦。 我有一个没有源的调试apk。“开始activity”类是 我把它安装在 然后使用以下build.gradle和androidmanifest.xml创建一个新项目 null null 但当我运行测试时,它报告找不到“com.restfriedchicken.android.mainActivity”: 看来仪器没有成功?

  • 我想运行一个junit4测试套件,并在构建中使用过滤器指定它。gradle,但gradle(版本2.10)没有找到测试。有没有办法在gradle中运行指定的junit测试套件? 我的测试套件类 build.gradle

  • 我正在使用gradle来构建和测试我的应用程序。我使用命令 但是当从eclipse中运行时,测试工作完美,当使用gradle运行时失败。我的测试定义如下,它找不到的属性文件在这两个文件夹中: /home/user/Development/git/myproject/src/main/java/com/mycompany/config/home/user/Development/git/myproj

  • 问题内容: 我尝试使用Maven创建一个测试项目,并且单元测试工作正常。但是,当尝试对j2ee项目执行相同操作时,surefire无法找到单元测试。测试位于正确的目录(src / test / java)中,并且正在对其进行编译。 junit测试位于src / test / java / unit / TestAddition.java pom.xml中的surefire插件配置不包含/排除任何文