Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何写单元测试 #502

Open
cloud13th opened this issue Jul 17, 2020 · 0 comments
Open

如何写单元测试 #502

cloud13th opened this issue Jul 17, 2020 · 0 comments

Comments

@cloud13th
Copy link

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,请问有解决办法么?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant