• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reg sequence of tests

 
Ranch Hand
Posts: 38
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 40
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic