IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> java.lang.NullPointerException at org.apache.struts2.StrutsTestCase.getActionProxy -> 正文阅读

[开发测试]java.lang.NullPointerException at org.apache.struts2.StrutsTestCase.getActionProxy

作者:recommend-item-box type_blog clearfix

使用junit测试structs的action时出现
java.lang.NullPointerException
at org.apache.struts2.StrutsTestCase.getActionProxy

代码:

package action;

import dao.UserDao;
import org.apache.struts2.StrutsSpringTestCase;
import org.apache.struts2.StrutsTestCase;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import com.opensymphony.xwork2.ActionProxy;
import service.UserService;

import java.util.HashMap;
import java.util.Map;

import static org.easymock.EasyMock.replay;

public class LoginActionTest extends StrutsSpringTestCase  {

    // 重写父类方法,指定配置文件的名字
    protected String[] getContextLocations() {
        return new String[] { "struts.xml", "applicationContext.xml" };
    }

    private UserService userService;

    @Before
    public void setUp() throws Exception{
        userService = EasyMock.createMock(UserService.class);
    }


    @Test
    public void testExecute() throws Exception {

        ActionProxy proxy = getActionProxy("/loginAction");
        LoginAction test = (LoginAction) proxy.getAction();
        assertNotNull(test);

        test.setUsername("u1");
        test.setPassword("1");
        Map session=new HashMap();
        test.setSession(session);
        test.setUserService(userService);
        EasyMock.expect(userService.loginVerify("u1","1")).andReturn(true);
        replay(userService);
        String result = proxy.execute();
        assertEquals("success", result);
    }
}

报错:

java.lang.NullPointerException
	at org.apache.struts2.StrutsTestCase.getActionProxy(StrutsTestCase.java:131)
	at action.LoginActionTest.testExecute(LoginActionTest.java:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at junit.framework.TestCase.runTest(TestCase.java:176)
	at junit.framework.TestCase.runBare(TestCase.java:141)
	at junit.framework.TestResult$1.protect(TestResult.java:122)
	at junit.framework.TestResult.runProtected(TestResult.java:142)
	at junit.framework.TestResult.run(TestResult.java:125)
	at junit.framework.TestCase.run(TestCase.java:129)
	at junit.framework.TestSuite.runTest(TestSuite.java:252)
	at junit.framework.TestSuite.run(TestSuite.java:247)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

原因是我覆盖了继承的StrutsSpringTestCase的setUp函数
需要加上super.setUp()才能正确

    @Before
    public void setUp() throws Exception{
        super.setUp();
        userService = EasyMock.createMock(UserService.class);
    }
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2022-05-05 11:49:55  更:2022-05-05 11:50:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 11:42:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码