Skip to content

如何写单元测试 #502

@cloud13th

Description

@cloud13th

jeesite旧版中,如何使用Junit写controller和service的单元测试用例呢?
以下是我写的单元测试例子:
`
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/applicationContext.xml"})
public class HousingTypeRestTest extends AbstractJUnit4SpringContextTests {

@Autowired
protected WebApplicationContext context;

private MockMvc mockMvc;

@Before
public void init() {
    mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}

@SneakyThrows
@Test
@Rollback
public void getCommentInfos() {
    mockMvc.perform(
            MockMvcRequestBuilders.get("/rest/abc")
                    .accept(APPLICATION_JSON_UTF8)
    )
            .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
}

}
对应的controller代码为
@RequestMapping("/rest")
@controller
public class HelloRest {

@RequestMapping(value = "/abc")
@ResponseBody
public String hello() {
    return "Hello World.";
}

}
但是启动之后,报错误信息为:
java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)

...
`

查找了下,发现web.xml中配置的servletorg.glassfish.jersey.servlet.ServletContainer,但是官方也是这么配置的,所以咱也不敢乱动。

so,请问有解决办法么?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions