Checkstyle java

Bolaaro24 -  
 Bolaaro -
Bonjour,
J'ai un devoir qui consiste a corriger un code afin qu'il passe le checkstyle. Il y a deux erreurs que je n'arrive pas a corriger:
-Ligne 1:Package Names: Le nom 'Devoir2' n'est pas conforme à l'expression '^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$'.
-Ligne 5:Les classes utilitaires ne doivent pas avoir de constructeur par défaut ou public.

package Devoir2;
/**
* Ma class devoir.
*/
public class Calcul {
    /**
     * Calcul la somme de deux nombres.
     * @param a est un final int
     * @param b est un final int
     * @return a+b
     */
    public static int somme(final int a, final int b) {
        return a + b;
        }
    /**
     * @param a est un final int
     * @param b est un final int
     * @return a/b si b>=10 sinon b
     */
    public static int maFonction(final int a, final int b) {
        final int c = 10;
        if (b >= c) {
            return a / b;
            }
        return b;
        }
    /**
     * @param a est un final int
     * @param b est un final int
     * @return a / b si b != 0
     * @throw IllegalArgumentException si b == 0
     */
    public static int division(final int a, final int b) {
        if (b == 0) {
            throw new IllegalArgumentException("b ne doit pas etre 0");
        }
        return a / b;
    }
}


Configuration: Windows / Chrome 77.0.3865.120
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Bonjour,

Tu devrais t'aider des messages d'erreurs, ils sont là pour t'indiquer ce qui ne va pas.

-Ligne 1:Package Names: Le nom 'Devoir2' n'est pas conforme à l'expression '^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$'.
Un package doit commencer par une minuscule.

-Ligne 5:Les classes utilitaires ne doivent pas avoir de constructeur par défaut ou public.
Ajoutes un constructeur privé.
0
Bolaaro24
 
Merci. Pour la réponse.
J'ai ajouté un constructeur privé mais dès que je lance tests unitaires, il me dit qu'il ne peut pas accéder au constructeur et me propose de changer le type de constructeur
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020 > Bolaaro24
 
C'est une classe utilitaire, donc tu n'as pas besoin ni d'objets, ni de constructeur, il faut appeler les méthodes static directement.
0
Bolaaro24
 
Merci, je m'en suis finalement sorti mais j'ai un autre probleme. Les tests marchent bien sur eclipse mais ne marchent pas sur ligne de commande maven.
Voici les tests:
import org.junit.Rule;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.rules.ExpectedException;


class CalculTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();
    /** test du constructeur. **/

  @Test
  public void testConstructeur() {
    new Calcul();
  }

  /** test de la methode somme. **/
  @Test
  public void testSomme() {
    Assertions.assertEquals(5, Calcul.somme(3, 2));
  }

  /** test de la methode division. **/
  @Test
  public void testDivision() {
   Assertions.assertEquals(4, Calcul.division(8, 2));
  }

  @Test
  public void testMafonction_b_plusPetit10() {
    Assertions.assertEquals(7, Calcul.maFonction(12, 7));
  }

  @Test
  public void testMafonction_b_NonPlusPetit10() {
   Assertions.assertEquals(2, Calcul.maFonction(24, 12));
  }

  @Test
  public void should_throw_exception_when_divide_bt_zero() {
      thrown.expect(IllegalArgumentException.class);
      thrown.expectMessage("b ne doit pas etre 0");
      Assertions.assertEquals(4, Calcul.division(8, 0));
}
}



et voici le pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <name>MyProject</name>
  <description>Mon nouveau projet</description>
  <version>1.1</version>
  <groupId>default</groupId>
  <artifactId>my-project</artifactId>

  <dependencies><!-- dependendance du projet -->
    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.3.1</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins><!-- lister les plugins et leur version permet d'eviter que maven prenne celui qui trouvera par defaut -->

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
    <release>7</release>
    </configuration>
	</plugin>

      <plugin>
	<artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
      </plugin>

      <plugin><!-- pour l'execution des tests -->
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>3.0.0-M3</version>
	<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
      </plugin>
      
      <plugin><!-- pour la generation de rapports -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.7.1</version>
        <configuration>
          <locales>fr,en</locales><!-- en francais par defaut -->
        </configuration>
      </plugin>

      <plugin><!-- pour la couverture de code avec JaCoco lors de l'exécution des tests JUnit -->
	<groupId>org.jacoco</groupId>
	<artifactId>jacoco-maven-plugin</artifactId>
	<version>0.8.4</version>
	<executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
	</executions>
      </plugin>


      <plugin><!-- pour l'analyse avec checkstyle -->
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-checkstyle-plugin</artifactId>
	<version>3.0.0</version>
      </plugin>

    </plugins>
  </build>

  <reporting><!-- on indique ici les rapports qu'on veut obtenir lors de la phase de generation de rapports -->
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <reportSets>
          <reportSet>
            <reports>
              <report>report</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>


      <plugin><!-- pour avoir le rapport checkstyle -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>3.0.0</version>
        <reportSets>
          <reportSet>
            <reports>
              <report>checkstyle</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>

      <plugin><!-- pour avoir le lien au code source dans les rapports -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jxr-plugin</artifactId>
        <version>2.3</version>
      </plugin>
      
      <plugin><!-- pour la verification du code avec PMD -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.12.0</version>
      </plugin>

      <plugin><!-- pour avoir le rapport surfire (execution des tests) -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>3.0.0-M3</version>
      </plugin>

    </plugins>
  </reporting>

  <properties>
    <!-- encodage du code source -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

</project>

0
Bolaaro24
 
Il m'envoie une erreur en disant que le package org.junit doesn't exist
0
KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020 > Bolaaro24
 
Dans quel répertoire as tu mis ta classe de tests ?
Ils devraient être dans src/test/java, car le <scope>test</scope> ne s'applique pas au src/main/java
De plus je t'invite à ajouter à chaque classe un nom de package commençant par ton groupId et à mettre ta classe de test public.
0