aspose file tools*
The moose likes Testing and the fly likes Reg sequence of tests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Reg sequence of tests" Watch "Reg sequence of tests" New topic
Author

Reg sequence of tests

Meena Ajay
Ranch Hand

Joined: May 28, 2008
Posts: 38

Hi,
Greetings !
I am not sure if someone else has already posted this query...nevertheless, i am posting it because i recently faced an issue in using Mockito with Junit tests.

In my project Mockito and Junit are used for unit testing. In certain cases, a mix of mock objects and Autowired spring objects are used in combination for the tests.

I have some common resource which is being sourced using Autowired Spring beans and I have a sequence of test methods in the same test class.

The observation is that when the Junit tests on the class are executed from Eclipse directly , they execute properly. But when I run using the maven test command, i find that these tests run in parallel , and each test method is trying to source the same resource (Solace connection) and they fail.

On browsing, I found that there is not a straight forward way to execute the test methods in sequence by using some configuration in Junit (specify the order of test methods)(I am not sure of this though, as I could have missed the info which is possibly available in some site). Even if the nature of unit tests is such that each test is supposed to run independent of each other, in my case, it can be considered as an exception scenario where order of test methods in the class needs to be pre-defined.

Does TestNG and Mockito support such a feature ?

Also is there a way to verify that static methods are invoked inside mock objects similar to how one can verify invocation of non-static methods to ensure that these methods are invoked on mock objects

Thanks in advance
Regards,
Meena


Cheers, Meena
OCPJP 6
Tomek Kaczanowski
author
Ranch Hand

Joined: Oct 26, 2005
Posts: 40

Hi Meena,

Meena Ajay wrote:Hi,
In my project Mockito and Junit are used for unit testing. In certain cases, a mix of mock objects and Autowired spring objects are used in combination for the tests.

I have some common resource which is being sourced using Autowired Spring beans and I have a sequence of test methods in the same test class.

The observation is that when the Junit tests on the class are executed from Eclipse directly , they execute properly. But when I run using the maven test command, i find that these tests run in parallel , and each test method is trying to source the same resource (Solace connection) and they fail.

I do not think I can help you with this specific scenario, but one thing that bothers me is what you say about parallel execution of tests via Maven. This is unexpected - as far as I know, the surefire plugin (which is what Maven uses to execute tests) by default does not use parallel execution (see surefire docs). Maybe it is misconfigured?

Meena Ajay wrote:On browsing, I found that there is not a straight forward way to execute the test methods in sequence by using some configuration in Junit (specify the order of test methods)

JUnit believes that dependencies between tests are evil. It is ok for the vast majority of test cases, however as you can see yourself sometimes it is not. Anyway, I do not think there is a way to force JUnit to do that, at least not without tweaking its runners or writing your own one.
And as for TestNG the answer is "yes, this is possible". In fact, this is one of the features TestNG offers, which makes it much more suitable than JUnit for tests other than unit tests (it is quite common for integration tests to rely on each other - e.g. one tests add something to database, the other relies on the fact that this thing is there). With TestNG you can specify order of execution in many ways, for example:
* you can say that method A should be invoked after method B
* you can say that method A should be invoked after a group of tests B is finished
* you can say that a test class A should be invoked after a group of tests B is finished
* and probably few more similar things which are described in TestNG docs
A word of caution - "invoked after" means only this that B will be invoked after A, but not necessarily immediately after A!

Meena Ajay wrote:Also is there a way to verify that static methods are invoked inside mock objects similar to how one can verify invocation of non-static methods to ensure that these methods are invoked on mock objects

Do you mean mocking of static methods? If so, please visit this thread


Tomek Kaczanowski
Book author: Practical Unit Testing with TestNG and Mockito
http://practicalunittesting.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg sequence of tests