Exemple de tâche Ant et jUnit

Exemple de tâche Ant et jUnit

Dans ce didacticiel, nous allons vous montrer comment exécuter un test junit dans Ant build.

junit tutorials

1. Lancer un test unitaire

build.xml



    

    
    

    
    

    
        
        
    

  

2. Exécuter un lot de test unitaire

build.xml



  

    
    
    

    
    

    
        
            
        
    

  

3. Exemple

Un exemple d'application Web pour vous montrer comment exécuter un test junit.

3.1 Return a message

MessageGenerator.java

package com.example.message;

import org.springframework.stereotype.Component;

@Component
public class MessageGenerator {

    public String getWelcomeMessage() {
        return "welcome";
    }

}

3.2 Two junit test cases to test above class.

TestMessage.java

package com.example.test;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.example.message.MessageGenerator;

public class TestMessage {

    @Test
    public void test_welcome_message() {
        MessageGenerator obj = new MessageGenerator();
        assertEquals("welcome", obj.getWelcomeMessage());
    }

}

TestMessage2.java

package com.example.test;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.example.message.MessageGenerator;

public class TestMessage2 {

    @Test
    public void test_welcome_message_2() {
        MessageGenerator obj = new MessageGenerator();
        assertEquals("welcome", obj.getWelcomeMessage());
    }

}

3.3 Use ivy to get the project dependencies, and declared the project scope.

ivy.xml


    

    
        
        
        
    

    
        
    

3.4 Run unit test

build.xml


    
        Running junit Test
    

    
    
    
    
    
    
    
    

    
    
        
        

        
        
        

    

    
    
        
        
            
        
    

    
    

      

        
        

        
        
            
                
            
        

      
    

    
    
        
        
        
    

    
    
        
        
    

    


Run it

$ ant junit

Terminé.

Télécharger le code source

Téléchargez-le -AntSpringMVC-Junit-Example (42 Ko)

Références