Java 使用SpringMVC进行JUnit测试:初始化错误
这是我第一次尝试JUnit测试和SpringMVC。 我为bean创建了一个测试配置类,为controller创建了一个测试类。执行测试时,出现测试失败,显示Java 使用SpringMVC进行JUnit测试:初始化错误,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,这是我第一次尝试JUnit测试和SpringMVC。 我为bean创建了一个测试配置类,为controller创建了一个测试类。执行测试时,出现测试失败,显示初始化错误,未找到类定义错误。 下面是我的TestBeanConfig.java文件 package com.mkyong.controller; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation
初始化错误,未找到类定义错误
。
下面是我的TestBeanConfig.java
文件
package com.mkyong.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
@Configuration
@ComponentScan(basePackages = "com.mkyong.controller")
public class TestBeanConfig {
@Bean
MultiplicationService multiplicationservice() {
return new MultiplicationService();
}
@Bean
NumberDAO numberDao() {
NumberDAO numberdao =new NumberDAO();
return numberdao;
}
}
和MultiplicationServiceTest.java
package com.mkyong.controller;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={TestBeanConfig.class})
public class MultiplicationServiceTest {
@Autowired
private MultiplicationService multiplytestobject;
MockMvc mockMvc ;
@Autowired
private WebApplicationContext wac;
public MultiplicationServiceTest() {
}
public MultiplicationService getMultiplytestobject() {
return multiplytestobject;
}
public void setMultiplytestobject(MultiplicationService multiplytestobject) {
this.multiplytestobject = multiplytestobject;
}
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testHandleMultiply() throws Exception {
mockMvc.perform(post("/multiply").param("number1","7").param("number2","7"))
.andExpect((model().attribute("result", "49")))
.andExpect(redirectedUrl("/multiply.jsp"));
}
}
错误跟踪:
Tests in error:
initializationError(com.mkyong.controller.MultiplicationServiceTest): Failed to instantiate [org.springframework.test.context.web.WebDelegatingSmartContextLoader]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.626s
[INFO] Finished at: Sun Jan 28 15:40:26 IST 2018
[INFO] Final Memory: 16M/180M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.9:test (default-test) on project bmfinal: There are test failures.
[ERROR]
谢谢。将此添加到maven pom.xml依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
javax.servlet
javax.servlet-api
3.0.1
假如
这应该提供项目搜索的类
如果有帮助,请告诉我。它已解决初始化错误。然而,出现了其他错误。测试出错:testHandleMultiply(com.mkyong.controller.MultiplicationServiceTest):未能加载ApplicationContext这将导致不同的场景。你用过applicationContext.xml吗?你是从Mkyong教程开始的,不是吗?哪一个?我没有使用applicationContext.xml。我用mkyong counter webapp项目启动了springmvc。为了进行测试,我参考了其他一些源代码。您使用的是Eclipse还是STS?Eclipse。在编写TestBeanConfig.java之后,我是否需要使用applicationContext.xml进行配置?