MvcTest.java
package main.java.com.atguigu.crud.test;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.log.Log;
import com.mysql.jdbc.log.LogFactory;
import main.java.com.atguigu.crud.bean.Employee;
import main.java.com.atguigu.crud.service.EmployeeService;
/***
- 使用Spring测试模块中提供的测是请求功能,测试curd请求正确性
- 报错:java.lang.NoClassDefFoundError:javax/servlet/SessionCookieConfig
- 注:Spring4测试的时候,需要servlet3.0的支持
- @author lfy
*/
//SpringJUnit4ClassRunner.class是Spring提供的单元测试模块
//所有的text都是@RunWith运行
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration指定Spring配置文件的位置,多个配置文件用逗号分开classpath:dispatcherServlet.xml
@ContextConfiguration(locations={“classpath:applicationContext.xml”,“classpath:dispatcherServlet.xml”})
@WebAppConfiguration
public class MvcTest {
//虚拟mvc请求
MockMvc mockMvc;
//org.junit.Test中的@Before
/*
@Autowired
EmployeeService employeeService;*/
//传入Springmvc的ioc
@Autowired
WebApplicationContext context;
@Before
public void initMokcMvc () {
//将mockMvc创建出来
mockMvc= MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void testPage() throws Exception{
System.out.println("测试开始");
//模拟请求通过.andReturn()拿到返回值
MvcResult result= mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "5")).andReturn();
//请求成功后,请求域中会有pageInfo;我们可以取出pageInfo进行验证;
MockHttpServletRequest request=result.getRequest();
//强转PageInfo
//List<Employee> pi=(List<Employee>) request.getAttribute("pageinfolist");
/*
List<Employee> emps=employeeService.getAll();
for (Employee employee : emps) {
System.out.println("ID"+employee.getEmpId()+"name"+employee.getEmpName());
}*/
PageInfo pi= (PageInfo) request.getAttribute("pageinfolist");
System.out.print(pi);
System.out.println("当前页码"+pi.getPageNum());
System.out.println("总页码"+pi.getPages());
System.out.println("总记录数"+pi.getTotal());
System.out.println("在页面需要连续显示的页码");
int[] nums =pi.getNavigatepageNums();
for (int i : nums) {
System.out.print(""+i);
}
//获取员工数据
List<Employee> list=pi.getList();
for (Employee employee : list) {
System.out.println("ID"+employee.getEmpId()+"name"+employee.getEmpName());
}
}
}
EmployeeController.java
package main.java.com.atguigu.crud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import main.java.com.atguigu.crud.bean.Employee;
import main.java.com.atguigu.crud.service.EmployeeService;
/***
- 处理员工CRUD请求
- @author lfy
/
@Controller
public class EmployeeController {
@Autowired
EmployeeService employeeService;
/**
* 查询员工数据请求
* @return
*/
@RequestMapping("/emps")
public String getEmps(@RequestParam(value=“pn”,defaultValue=“5”)Integer pn,Model model){
//为了分页简单,引入PageHelper分页插件
//查询前调用分页方法PageHelper.startPage(从第几页开始查,一页几条数据);传入页码跟每页显示的数量,
PageHelper.startPage(pn,5);
//PageHelper紧跟的查询就是一个分页查询
List emps=employeeService.getAll();
//使用PageInfo包装查询结果,只需要将PageInfo交给页面就行了
//封装了详细的分页信息,包括有我们查询出来的数据,传入连续显示的页数
System.out.println(“对象:”+emps);
PageInfo page=new PageInfo(emps,5);
model.addAttribute(“pageinfolist”,page);
return “list”;
}
}
EmployeeService.java
package main.java.com.atguigu.crud.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import main.java.com.atguigu.crud.bean.Employee;
import main.java.com.atguigu.crud.dao.EmployeeMapper;
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
/***
* 查询所有员工
* @return
*/
public List<Employee> getAll(){
return employeeMapper.selectByExampleWithDept(null);
}
}
运行报错提示没有取到值,request.getAttribute(“pageinfolist”);为null。请问大佬是什么原因
java.lang.NullPointerException
at main.java.com.atguigu.crud.test.MvcTest.testPage(MvcTest.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
最后问题解决了我这边是写错了个东西:我把include-filter 写成了exclude-filter,排坑了