gpt4 book ai didi

java - 如何解决 Spring Controller 测试用例中的 NoSuchMethodError?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:14:35 29 4
gpt4 key购买 nike

java.lang.NoSuchMethodError: 
org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/
runner/Description;Ljava/lang/Throwable;)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestM­ethod(SpringJUnit4ClassRunner.java:
155)

并为 Controller 编写测试用例,例如,为 Spring Controller 类新编写测试用例:

TestXController.java

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"file:D:/ABC/src/main/webapp/WEB-INF/
xyz-servlet.xml",
"file:D:/ABC/src/main/webapp/WEB-INF/xyzrest-servlet.xml"})

public class TestXController {

@Inject
private ApplicationContext applicationContext;

private MockHttpServletRequest request;
private MockHttpServletResponse response;
private HandlerAdapter handlerAdapter;
private XController controller;

@Test
public void setUp() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
// I could get the controller from the context here
controller = new XController();
}

@Test
public void testgoLoginPage() throws Exception {

request.setAttribute("login", "0");

final org.springframework.web.servlet.ModelAndView mav = handlerAdapter.handle(request, response, controller);
assertViewName(mav, null);
assertAndReturnModelAttributeOfType(mav, "login", null);
}

@Test
public void testgoHomePage(){

org.springframework.web.servlet.ModelAndView mav =null;

request.setAttribute("success1", "1");
request.setAttribute("success", "1");

try {
mav = handlerAdapter.handle(request, response, controller);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

assertViewName(mav, null);
assertAndReturnModelAttributeOfType(mav, "home",null);
}

谁能指导我为 Spring 编写测试用例 Controller 类,或任何代码示例链接。

感谢并问候,Venu Gopala Reddy。

最佳答案

是的,请确保您使用的是正确版本的 JUnit。我认为与强制您使用 JUnit 4.4 的 Spring 测试 JAR 不匹配。

关于java - 如何解决 Spring Controller 测试用例中的 NoSuchMethodError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10651575/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com