Return-Path: X-Original-To: apmail-felix-users-archive@minotaur.apache.org Delivered-To: apmail-felix-users-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B1B461108E for ; Tue, 16 Sep 2014 14:23:16 +0000 (UTC) Received: (qmail 14531 invoked by uid 500); 16 Sep 2014 14:23:16 -0000 Delivered-To: apmail-felix-users-archive@felix.apache.org Received: (qmail 14467 invoked by uid 500); 16 Sep 2014 14:23:16 -0000 Mailing-List: contact users-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@felix.apache.org Delivered-To: mailing list users@felix.apache.org Received: (qmail 14454 invoked by uid 99); 16 Sep 2014 14:23:15 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Sep 2014 14:23:15 +0000 X-ASF-Spam-Status: No, hits=1.0 required=5.0 tests=FORGED_YAHOO_RCVD,RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of david_jencks@yahoo.com designates 98.138.91.74 as permitted sender) Received: from [98.138.91.74] (HELO nm1-vm0.bullet.mail.ne1.yahoo.com) (98.138.91.74) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 Sep 2014 14:23:10 +0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s2048; d=yahoo.com; b=trF0eeOfUeLWnAcrcJlAIqBhPm32HjJkiukRBLtgOqYRddJ9wl9trPuj0YbNK1iIXh1apnQ8nu/dn0WM5m55kgQtr+gXT3Y/ggsGNhmLfyv3GRyMteSjzdGcNTTJYPBW4E5L6Os/KBH181RfP/zGSt/UgxQNNZdJhXVwDCexrvutYXZQVsvRENwleZDF2Ea76b2D8gDefJBdwirxnYav7udSN+ogyZW198cNEqgBIXoI4n394pL+Rtbnm9Wbf9ljOTUb34ptwyKjzvoZR4OhCZ1e0k4WThYll2mcQuwJtV5b3cTTbsyKGs837WmZXvl1paOEEtf9i2BHDxS0UQezdg==; Received: from [98.138.100.102] by nm1.bullet.mail.ne1.yahoo.com with NNFMP; 16 Sep 2014 14:22:49 -0000 Received: from [98.138.84.41] by tm101.bullet.mail.ne1.yahoo.com with NNFMP; 16 Sep 2014 14:22:49 -0000 Received: from [127.0.0.1] by smtp109.mail.ne1.yahoo.com with NNFMP; 16 Sep 2014 14:22:49 -0000 X-Yahoo-Newman-Id: 437921.15436.bm@smtp109.mail.ne1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: j8VxC4gVM1myy14HzDlfOEKeATqUzD0w6BTPu8bhIxEBwj8 uxagSV9jiggIhgFk9FNwNDPIDj6PQ4ezXLnFbT2hNDhnsQadamaJwu92.EBl LJqYEmA97lwAl8sp9vaXoUOSw2xnq1Ghf093Mb6LFCeFdTdqxYG.Y8MC2wLU rpUPLWv5YDythlFKLi1BpLYzUHPY9tq5qC4W3EMZPmsmfwzpFPViXSC9nY36 A.pRpLoR6oncO84VwWssQZ0DKTorlLtLR_3BObKm4a6qWBQqRBnWJbD4Czzp mb_Yuwv0URLFUmDGog3RjeEDmUmbMqcpm0a.cj6hImhSVZLDYGJZEjBqsZXK BA7i4HzemOIIv._Jd3s6DHQUrj6iu8HJ0Ka8fk84hnu6MJyVxxX89ibsQb8Z KMELrCStPbsIi6HvHOubhQcVWWyvQ4pY3uld4ClNcqyWHsRgyJdwvSS6hRwY SFov5Xq4Gymx7Y_REMAgosCnx9KpUPNHba6JOy.jZu2xFjxs36yMT525k7Km VGyRjwBIbbhQi9vOpJRT7_F0M7Gn6h0Hkig-- X-Yahoo-SMTP: .9oIUzyswBANsYgUm_5uPui0skTnzGJXJQ-- Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: testing a service, mocking dependencies From: David Jencks In-Reply-To: Date: Tue, 16 Sep 2014 07:22:47 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: References: To: users@felix.apache.org X-Mailer: Apple Mail (2.1510) X-Virus-Checked: Checked by ClamAV on apache.org You don't provide enough information to know what you are doing, and I = don't understand what you mean by a unit test. If you are writing a DS component and using Felix DS annotations, note = that the @Reference on a field without accessors results in byte code = generation of accessor methods. You might want to write them yourself. Is your "unit test" running in a DS container? =20 If not, then you need to simulate the calls to the event = (bind/updated/unbind) and lifecycle (activate/modified/deactivate) = methods that the container will make in your unit test. After all the = "stop" methods (deactivate and unbind) are called, the container will = discard the component instance. If you are running in a DS container then register your B as a service = and the container will take care of calling the (generated, with your = current code)bind/unbiind methods. LIke every other component framework that accesses apparently invisible = members during runtime, DS calls setAccessible internally so it can = access them. The spec may support field injection in the future and it = will be supported using setAccssible pretty much exactly as David B. = suggests. david jencks On Sep 16, 2014, at 6:59 AM, Konstantine Kougios = wrote: > Well, that=B9s similar to the forTesting factory I mention but with = the > drawback that the state of A is corrupted until the JVM terminates. By > corrupted I mean that my mock will be part of A until either the jvm = stops > or something else injects a different thing to A.b. If the mock is set > this way while running in-container tests, A state is corrupted. >=20 >=20 > On 16/09/2014 14:56, "David Bosschaert" = wrote: >=20 >> Why not set the field b in your object manually to the mock B for = unit >> testing? >>=20 >> Just assign it to the field. If you insist on having it private you >> can call 'Field.setAccessible(true)' in your unit test and assign it >> using reflection... >>=20 >> Best regards, >>=20 >> David >>=20 >> On 16 September 2014 14:39, Konstantine Kougios >> wrote: >>> Hi, say I got a service A, which has a >>>=20 >>> @Reference private B b; >>>=20 >>> Now I want to write a unit test and mock B, how can I inject the = mocked >>> dependency? >>>=20 >>> Only reasonable thing I found so far is to have a static factory = method >>> on A, public static A forTesting(B b) { =8A } >>>=20 >>> Thanks, >>>=20 >>> Kostas >>=20 >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org >> For additional commands, e-mail: users-help@felix.apache.org >>=20 >=20 >=20 > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@felix.apache.org > For additional commands, e-mail: users-help@felix.apache.org >=20 --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@felix.apache.org For additional commands, e-mail: users-help@felix.apache.org