TL;DR
Yes, it is possible to run tests selectively with junit, but how you do it depends on how you start such an execution.
Different ways to run tests
The simplest way to perform unit tests during development is through your IDE. Eclipse, Intellij and Netbeans allow you to click on a specific class or method and you will have some option like Run as Test or Test as Java Application and so on.
With Maven, the person responsible for carrying out the tests in the test
is the Maven Surefire plugin. It is possible:
Some continuous integration servers can also run tests automatically, but in this case the configuration should be done in the tool and not in the project itself.
Considerations
Unit tests should not be slow. If this is the case, first consider the possibility of rewriting the slowest ones. Remove unnecessary dependencies using Stubs, Mocks, or Fakes.
On the other hand, generally a slow test usually means that it is not really a unit test, but an integration test. A common case is tests that make remote calls to third-party systems, or boot the entire application on an embedded server.
In this case, such tests should be placed separately from the others, either using project configuration or even creating a separate project.
In the case of Maven or Gradle, an interesting strategy is to have a project composed of several modules (subprojects) within which one or more projects would be composed only by integration tests. They are still part of the same code base, so they are easily maintained in sync with the project, but at the same time live apart from the main source code and unit tests.
Can post relevant settings for your
pom.xml
? There are ways to do this with some plugins like surefire (everything will depend on what is running when you callmvn test
).– Anthony Accioly