Is it possible to test only specific classes?

4

I have two test classes:

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


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

My tests are taking a long time because all classes are being tested. There are updates that do not have to go through all the tests, for example, I modified the B class and I'm sure it will not affect the others, but I still need to test its behavior.

Is it possible to test only the B class? I know that if I take out @Test of the method the test will not run, but having to put / take every test is tiring and time-consuming.

    
asked by anonymous 09.12.2015 / 19:42

3 answers

6

TL; DR

Yes , you can selectively run tests with jUnit, but how you do this depends on how you start the run.

Different ways to run tests

The simplest way to run 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 options like Run as Test or Test as Java Application and so on.

With Maven , the test driver at test is Maven Surefire plugin . You can:

Some continuous integration servers can also run tests automatically, but in this case the configuration must be done in the tool and not in the project itself.

Considerations

Unit testing should not be slow. If this is the case, first consider 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 unitary but integration test. A common case is tests that make remote calls to third-party systems, or that initialize the entire application on an embedded server.

In such a case, such tests should be placed separately from the rest, either by designing the project or by 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 would be projects composed only by integration tests. They are still part of the same code base, so they are easily kept in sync with the project, but at the same time they live away from the main source code and unit tests.

    
10.12.2015 / 02:05
3

Good night renan

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

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

mvn -Dtest=ClasseTestB test

The second form is configuring in pom.xml to exclude a specific package or class from the 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 the Maven Surefire Plugin and the Maven Failsafe Plugin.

Ref1: link

Ref2: link

    
10.12.2015 / 02:06
3

You can also note the method or test class that should be ignored with @Ignore :

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

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

Optionally, you can specify the reason for 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 .

    
14.04.2016 / 06:33