[testng-users] TestNG and Easymock -

559 views
Skip to first unread message

mane_uk

unread,
Feb 28, 2012, 6:46:58 AM2/28/12
to testng...@googlegroups.com

Hi all,

I have been giving a task of converting come existent testing modules
from JUnit into TestNG. I am having problem to convert it when the file have
easymock on it though.

I am finding the following error:
FAILED CONFIGURATION: @BeforeMethod setUp
java.lang.IllegalStateException: no last call on a mock available
at org.easymock.EasyMock.getControlForLastCall(EasyMock.java:521)
at org.easymock.EasyMock.expect(EasyMock.java:499)
at tva.ValMultiStepTest.setUp(ValMultiStepTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:550)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:212)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:638)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:891)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1215)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:758)
at org.testng.TestRunner.run(TestRunner.java:613)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1170)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1095)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)

Below is my code in the ValMultiStepTest file:

<import session here> ...

public class ValMultiStepTest extends ValAbstractTest {
private final Date businessDate = new Day(2011, 7, 1);

@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {

<some code here>...

expect(merc.getBusinessDate()).andReturn(businessDate).anyTimes();
replay(merc);
}

@Test(groups... <all the tests here>

Below is my code in the ValAbstractTest file:
public abstract class ValAbstractTest {
protected final Merc merc = EasyMock.createMock(Merc.class);

<more code here> ...

The error points to the "expect" line on the ValMultiStepTest file.

So, question is, why does it work on JUnit and not on TestNG? What i am
doing wrong on the conversion (bear in mind I am using the automated tool
though on Eclipse to convert it)? How can I fix it?

Thanks for your help!!
--
View this message in context: http://old.nabble.com/TestNG-and-Easymock---tp33406112p33406112.html
Sent from the testng-users mailing list archive at Nabble.com.

Tomek Kaczanowski

unread,
Feb 28, 2012, 7:34:36 AM2/28/12
to testng...@googlegroups.com
Probably you need to move mock creation to @BeforeMethod or sth like
that. I guess the things worked when JUnit because it instantiated
test classes much more often then TestNG does.

--
Regards / Pozdrawiam
Tomek Kaczanowski
http://kaczanowscy.pl/tomek


2012/2/28 mane_uk <man...@hotmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "testng-users" group.
> To post to this group, send email to testng...@googlegroups.com.
> To unsubscribe from this group, send email to testng-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.
>

Cédric Beust ♔

unread,
Feb 28, 2012, 11:31:01 AM2/28/12
to testng...@googlegroups.com
Yup. You are creating your mock at initialization time, which means it only happens when the object is initialized. TestNG doesn't reinstantiate your test class before every test method, so your mock only gets initialized one.

Moving this to @BeforeMethod should fix the problem (it's also cleaner from a testing perspective).

Another thing: you are doing expect/replay in @BeforeMethod, which is a configuration method. Consider doing this in @Test method, so that the failures will be accurately reported as test failures.

One last thing: Eclipse will convert your tests automatically from JUnit to TestNG, you might want to start there (if you are not already doing that).

-- 
Cédric

mane_uk

unread,
Feb 29, 2012, 6:28:31 AM2/29/12
to testng...@googlegroups.com

Hi Tomek/Cedric,

Thanks for the reply.

I have changed the code as suggested, getting the easymock creation into
the @BeforeMethod. So my code now is looking like this for the
ValMultiStepTest file:

<import session here> ...

public class ValMultiStepTest extends ValAbstractTest {
private final Date businessDate = new Day(2011, 7, 1);

@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {

final Merc merc = EasyMock.createMock(Merc.class);

<some code here>...

expect(merc.getBusinessDate()).andReturn(businessDate).anyTimes();
replay(merc);
}

@Test(groups={"functest"})
public void testNormalTr() throws Exception {
testTr(swTr, "SW");
testTr(cfTr, "CF");
}

@Test(groups... <all other tests here>

I am getting the following error now:

java.lang.IllegalStateException: missing behavior definition for the
preceding method call:
Market.getBusinessDate()
Usage is: expect(a.foo()).andXXX()
at
org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:42)
at
org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:85)
at $Proxy6.getBusinessDate(Unknown Source)
at
validation.AbstractLegValRule.isLegApplicable(AbstractLegValRule.java:33)
at
validation.AbstractRuleValidator.validateLegs(AbstractRuleValidator.java:72)
at
validation.AbstractRuleValidator.validateTr(AbstractRuleValidator.java:45)
at
tva.MultiStepValAttributes.internalIsValuable(MultiStepValAttributes.java:90)
at tva.ValAbstractTest.testTrades(ValAbstractTest.java:39)
at tva.ValMultiStepTest.testH1(ValMultiStepTest.java:110)


at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)

at org.testng.internal.Invoker.invokeMethod(Invoker.java:699)


at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:891)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1215)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:758)
at org.testng.TestRunner.run(TestRunner.java:613)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1170)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1095)
at org.testng.TestNG.run(TestNG.java:1007)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)

Looking into the internet,all the information I came across is that the
error stated above seems to be related to missing the ".andReturn()" command
but this is not the case for this code. Any ideas of other problems that
could generate this error message?

@Cedric - About moving the expect/replay to @Test my task is literally
convert from JUnit into TestNG and make sure it works. I will though pass on
the message to the developers owner of this code about using the
expect/replay in @Test instead of @BeforeMethod. (Unless I need to do it to
fix the problem mentioned above - although I have tried and it didn't
work!!)

And, yes, I am using the automatically conversion using the Eclipse plugin!!
It is a fantastic tool. It does makes my life a lot simpler. It doesn't work
all the time, i.e.: the example above, but it is a great tool and extremely
helpful!! Great work on that!!

Cheers
Emanuel
=====================


Yup. You are creating your mock at initialization time, which means it only
happens when the object is initialized. TestNG doesn't reinstantiate your
test class before every test method, so your mock only gets initialized one.

Moving this to @BeforeMethod should fix the problem (it's also cleaner from
a testing perspective).

Another thing: you are doing expect/replay in @BeforeMethod, which is a
configuration method. Consider doing this in @Test method, so that the
failures will be accurately reported as test failures.

One last thing: Eclipse will convert your tests automatically from JUnit to
TestNG, you might want to start there (if you are not already doing that).

--
Cédric


On Tue, Feb 28, 2012 at 4:34 AM, Tomek Kaczanowski <
kaczanow...@gmail.com> wrote:

> Probably you need to move mock creation to @BeforeMethod or sth like
> that. I guess the things worked when JUnit because it instantiated
> test classes much more often then TestNG does.
>
> --
> Regards / Pozdrawiam
> Tomek Kaczanowski
> http://kaczanowscy.pl/tomek

--
View this message in context: http://old.nabble.com/TestNG-and-Easymock---tp33406112p33413299.html

mane_uk

unread,
Feb 29, 2012, 10:43:48 AM2/29/12
to testng...@googlegroups.com

Hi all,

Managed to partially fix it. Just inialized my variable in the main file
and added the creation of the easymock in the ValMultiStepTest file. So the
code looks like this:

<import session here> ...
public class ValMultiStepTest extends ValAbstractTest {
private final Date businessDate = new Day(2011, 7, 1);

@BeforeMethod(alwaysRun = true)
public void setUp() throws Exception {

merc = EasyMock.createMock(Merc.class);
<some code here>...
expect(merc.getBusinessDate()).andReturn(businessDate).anyTimes();
replay(merc);
}

@Test(groups={"functest"})

public void testHF11() throws Exception {
floatingLeg.setCompoundingPeriod("3m");

testTr(swTr, "SW", HF11);
testTr(cfTr, "CF", HF11);
}

@Test(groups... <all other tests here>

ValAbstractTest file:
public class ValAbstractTest {
Merc merc;
<code>


I am getting some AssertionError now.

java.lang.AssertionError: Actual violations: [DefaultAttributeViolation
[name=ACCRUAL_FRACTION, operand=0.0, type=EQUAL, value=0, ruleId=H8],
DefaultAttributeViolation [name=COMPOUNDING_PERIOD, operand=3.0M,
type=NOT_EQUAL, value=6.0M, ruleId=HF11]] expected:<[HF11]> but was:<[H8,
HF11]>
at org.testng.AssertJUnit.fail(AssertJUnit.java:59)
at org.testng.AssertJUnit.failNotEquals(AssertJUnit.java:364)
at org.testng.AssertJUnit.assertEquals(AssertJUnit.java:80)
at tva.ValAbstractTest.testTrades(ValAbstractTest.java:46)
at tva.ValMultiStepTest.testHF11(ValMultiStepTest.java:156)

If you have any ideas what could be the problem it would be very helpful!!
Thanks

--
View this message in context: http://old.nabble.com/TestNG-and-Easymock---tp33406112p33414929.html

Cédric Beust ♔

unread,
Feb 29, 2012, 11:44:38 AM2/29/12
to testng...@googlegroups.com
Emanuel,

Can you post a small fully-contained class showing the problem so I can try to reproduce it?

Thanks.

-- 
Cédric




mane_uk

unread,
Mar 2, 2012, 8:37:04 AM3/2/12
to testng...@googlegroups.com

I found the problem, I was not starting my local variables inside the
@BeforeMethod. Once i have done voila!! Problem solved!!

Thanks anyway for all your help!!
Cheers
--
View this message in context: http://old.nabble.com/TestNG-and-Easymock---tp33406112p33428508.html

Reply all
Reply to author
Forward
0 new messages