Return-Path: X-Original-To: apmail-struts-user-archive@www.apache.org Delivered-To: apmail-struts-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 37F6A10250 for ; Thu, 29 Aug 2013 10:23:31 +0000 (UTC) Received: (qmail 42095 invoked by uid 500); 29 Aug 2013 10:23:29 -0000 Delivered-To: apmail-struts-user-archive@struts.apache.org Received: (qmail 42004 invoked by uid 500); 29 Aug 2013 10:23:29 -0000 Mailing-List: contact user-help@struts.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Struts Users Mailing List" Reply-To: "Struts Users Mailing List" Delivered-To: mailing list user@struts.apache.org Received: (qmail 41991 invoked by uid 99); 29 Aug 2013 10:23:28 -0000 Received: from minotaur.apache.org (HELO minotaur.apache.org) (140.211.11.9) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Aug 2013 10:23:28 +0000 Received: from localhost (HELO mail-pd0-f171.google.com) (127.0.0.1) (smtp-auth username lukaszlenart, mechanism plain) by minotaur.apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Aug 2013 10:23:28 +0000 Received: by mail-pd0-f171.google.com with SMTP id g10so263013pdj.16 for ; Thu, 29 Aug 2013 03:23:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=1LqzdLiZGHOeugtIkB09Okzasf5xYdt4Kb4wBFGmEmo=; b=SPcnbbLdjpFAgSAIy40kfVHJ+c6fhgSjoDFlK2xcCQyg/TMAuuCsGjB02ydlGMqyAL s8Pn0sJehCsnf3tD7gZcu7w8Tl9F92OD9NwrpMeEOxY6fTBMn7aP5t+Bc/wCSf5SEt2R 9X6rS4gjkZio1ig/wOh8a4r8AoHHD5i8YYAyQGIOpYZPOvBoZmIoJo7NErHFodWCIwZK fzT5qNmsOnPM/KL6gF/qW9rfRVrPXHaw27rqUhmVozLlKuVjqUo+yAjweuL7cScipcdj rB3DZRykfwgdale867fS5FLcUapvmW5Yk3XgkNfgjOtK+vBVnJDZ6nEnxoP+BC/DnpDS pW6w== X-Received: by 10.68.229.2 with SMTP id sm2mr2781426pbc.68.1377771807460; Thu, 29 Aug 2013 03:23:27 -0700 (PDT) MIME-Version: 1.0 Received: by 10.68.20.169 with HTTP; Thu, 29 Aug 2013 03:23:07 -0700 (PDT) In-Reply-To: References: From: Lukasz Lenart Date: Thu, 29 Aug 2013 12:23:07 +0200 Message-ID: Subject: Re: How to Mock getText("customerLabel") method in junit? To: Struts Users Mailing List Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2013/8/29 Mohit Gupta : > I am facing one issue while writing the junit of my struts 2 action > class.My action class has following code > snippet > > getText("customerLabel") > > While execution of junit i get below error though works fine thru > application(i think > Struts controller loads it while normal web flow which does not happe= n > during junit) > > java.lang.NullPointerException > at > com.opensymphony.xwork2.util.LocalizedTextUtil.findText(LocalizedTextUtil= .java:361) > at > com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.j= ava:208) > at > com.opensymphony.xwork2.TextProviderSupport.getText(TextProviderSupport.j= ava:123) > at com.opensymphony.xwork2.ActionSupport.getText(ActionSupport.java:103) > > I could get it working with a workaround by below code sniipet in my test > class and asiign it to my my action annotated with annotation TestedObje= ct > > > > action =3D new Myction() > { > @Override > public String getText(String textName) > { > return "mockedLabel"; > } > }; > > But i am looking for clean solution? Mocking as your solution is perfectly fine. If you want you can do it like that in setUp: ValueStack valueStack =3D container.getInstance(ValueStackFactory.class).createValueStack() ActionContext.setContext(new ActionContext()) ActionContext.getContext().setValueStack(valueStack) but it doesn't look more readable ;-) Regards --=20 =C5=81ukasz + 48 606 323 122 http://www.lenart.org.pl/ --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@struts.apache.org For additional commands, e-mail: user-help@struts.apache.org