小编Sau*_*abh的帖子

无法为Spring控制器的JUnit测试加载ApplicationContext

我想写一个测试用例来检查我的控制器(getPersons).这是服务器端代码.我有什么困惑我应该放在里面@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/app-contest.xml"})

其次,我遇到了一些错误:

无法加载应用程序上下文.找不到我在@ContextConfiguration中指定的路径[

我有这样的结构:

 restAPI
    *src/main/java
      com.company.controller
         personController.java
    *Test
      com.company.testController
         personControllerTest.java
    *src
      main
       webapp
         WEBINF
           app-context.xml


@Autowired
private PersonService personService;

@RequestMapping(value="/t2/{yy_id}/person", method=RequestMethod.GET)
@ResponseBody
public PersonInfo[] getPersons() {

    return personService.getPersons();
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:WEB-INF/app-context.xml"})
@WebAppConfiguration
public class PersonControllerTest  {


@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;

@Before
public void setup() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}

@Autowired
private PersonService personService;

@Test
public void getPersons() throws Exception {

    this.mockMvc.perform(get("/t2/1/person")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk());

}
Run Code Online (Sandbox Code Playgroud)

跟踪

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:103) …
Run Code Online (Sandbox Code Playgroud)

junit spring spring-mvc junit4 spring-mvc-test

36
推荐指数
2
解决办法
20万
查看次数

如何使用Junit测试弹簧控制器方法

嗨,我是Spring和Junit的新手.我的控制器中有一个方法.我想为这个方法编写Junit(getPersons()).

@Autowired
private PersonService personService;

@RequestMapping(value="/t2/{yy_id}/person", method=RequestMethod.GET)
@ResponseBody
public PersonInfo[] getPersons() {

    return personService.getPersons();
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我并以正确的方式指导我.请举个例子.

junit spring spring-mvc junit4 junit3

7
推荐指数
1
解决办法
1万
查看次数

如何使用HTML5 Web Audio API录制我的声音

我正在寻找一种录制我的声音的方法 - 根据录制的声音 - 向左或向右移动动画.

我听说HTML5有一个音频API,您可以通过它来录制您的声音,但我不知道如何做到这一点.

我将感谢能够帮助我或提出解决此问题的解决方案,建议,代码或指南的所有人.

html5 tquery html5-audio three.js

5
推荐指数
1
解决办法
6774
查看次数

java.lang.ClassCastException: java.lang.Integer 不能转换为 java.lang.Double

我在运行时遇到问题来编译这段代码,这向我抛出了一个错误,即 java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮我更正此代码,我会非常高兴

 double x;

 public Double getMethod() {

     HashMap hashmap= new HashMap();

     hashmap = SumCal();

     List  listabc = (List) hashmap.get("abclist");
     int total=(Integer) hashmap.get("all_total");
     x = (Double) listabc.get(0)*100/total;
     return x;
    }
Run Code Online (Sandbox Code Playgroud)

java arrays jakarta-ee

3
推荐指数
2
解决办法
6万
查看次数