Is it possible to test only specific classes?

Asked

Viewed 1,738 times

4

I have two classes of tests:

class A {
   @Test
   public void fazAlgo(){
      // ...
   }
}


class B {
   @Test
   public void fazAlgoMesmo(){
      // ...
   }
}

My tests are taking too long because all classes are being tested. There are updates that don’t need to go through all the tests, for example, I modified the class B and I’m sure she won’t affect the others, but I still need to test her behavior.

It is possible to test only the class B?
I know if I take @Test of the method the test will not run, but having to put/take each test is tiring and time consuming.

  • 1

    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 call mvn test).

3 answers

6


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.

  • 1

    I caught a little but I managed to solve, thanks for the help :)

3

Good night Renan

If you are using Maven, there are two ways to do this.

The first is using Single Test via command line, you choose any class you want to test.

mvn -Dtest=ClasseTestB test

The second way is by configuring in pom.xml to delete a package or class in a specific test round

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19</version>
    <configuration>
      <excludes>
        <exclude>**/CircleIT.java</exclude>
        <exclude>**/SquareIT.java</exclude>
      </excludes>
    </configuration>
  </plugin>
</plugins>

You can achieve the same result with both Maven Surefire Plugin and Maven Failsafe Plugin.

Ref1: https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

Ref2: https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html

  • Thanks for the help Douglas :)

3

The method or class of test to be ignored with @Ignore:

// Ignorando todos os testes na classe 'A':
@Ignore
class A {

   @Test
   public void testaAlgo(){
      // ...
   }
}

Optionally, you can specify why you are ignoring a particular test:

@Ignore("Motivo de estar ignorando o teste")
class A {   

   @Test
   public void testaAlgo(){
      //...
   }
}

In the Junit5, this annotation will be called @Disabled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.