2013-04-02 16 views
10

ragazzi! Ho una nuova domanda per te. Sto aggiungendo alcuni dati alla cache utilizzando diversi gestori di cache e sto affrontando il problema con esso. Lo sto facendo usando junit e spring. Quando eseguo il test, i metodi di test sono eseguiti in modo casuale, ma ho bisogno che vengano eseguiti in ordine. Come farlo?? Ecco alcuni codice e un output della console proving:come eseguire i metodi di test di prova di junit in ordine

Classe di servizio:

@Service("HelloCache") 
public class CacheServiceImpl implements CacheInterface { 
    @Autowired 
    @Qualifier("memcachedClient") 
    private MemcachedClient mBean; 

    public void Add(String key, Object object) { 
     mBean.set(key, 12, object); 
    } 

    public void Get(String key) { 
     mBean.get(key); 
    } 

    public void Delete(String key) { 
     mBean.delete(key); 
    }  
} 

Ecco il test:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "file:src/main/java/spring.xml") 
public class UsingMemcachedTest extends TestCase { 
    @Autowired 
    @Qualifier("HelloCache") 
    private CacheInterface emcached; 
    private byte[][] i = new byte[2500][3000]; 
    private String key = "j"; 
    @Test 
    public void testAddBulkObjects() { 
     System.out.println(""); 
     System.out.println("This is async BULK adding test"); 
     long time = System.currentTimeMillis(); 
     for (int k=1; k<=1000; k++) { 
      emcached.Add(key+k, i); 
     } 
     long time2 = System.currentTimeMillis(); 
     long timeE=time2-time; 
     System.out.println("Vremya add BULK objects: " + timeE); 
     System.out.println(""); 
    } 

    @Test 
    public void testGetBulkObjects() { 
     System.out.println(""); 
     System.out.println("This is getting BULK objects test"); 
     long time = System.currentTimeMillis(); 
     for (int k=1; k<=1000; k++) { 
      emcached.Get(key+k); 
     } 
     long time2 = System.currentTimeMillis(); 
     long timeE=time2-time; 
     System.out.println("Vremya Get object: " + timeE); 
     System.out.println(""); 
    } 

    @Test 
    public void testDeleteBulkObjects() { 
     System.out.println(""); 
     System.out.println("This is deleting BULK objects test"); 
     long time = System.currentTimeMillis(); 
     for (int k=1; k<=1000; k++) { 
      emcached.Delete(key+k); 
     } 
     long time2 = System.currentTimeMillis(); 
     long timeE=time2-time; 
     System.out.println("Vremya delete object: " + timeE); 
     System.out.println(""); 
    } 

E l'output:

This is deleting BULK objects test 
Vremya delete object: 137 


This is getting BULK objects test 
Vremya Get object: 703 


This is async BULK adding test 
Vremya add BULK objects: 87681 

prega, AIUTO !! =)

+3

Perché li vuoi eseguiti in ordine? I test dovrebbero essere in grado di funzionare in qualsiasi ordine, IMO. Potresti sempre farlo alla vecchia maniera e creare un TestSuite. –

+0

Ho bisogno di farlo, perché sto aggiungendo alcuni dati nella cache, quindi recuperarlo dalla cache e solo dopo eliminarlo. Ecco perchè. – PAcan

risposta

8

JUnit non fa promesse riguardo all'ordine in cui vengono eseguiti i test. L'ordine sarà diverso a seconda dell'ambiente e del contesto in cui vengono eseguiti i test.

Per questo motivo (e altri) è considerato molto negativo il test di progettazione per l'ordine di influenzare il comportamento dei test. Puoi utilizzare @Prima di andare a dare una lavagna pulita con cui lavorare e quindi eseguire qualsiasi impostazione per un particolare test come parte di quel test.

La risposta accettata per la seguente questione dà una buona spiegazione e collegamenti ad alcune risorse utili: How to run test methods in specific order in JUnit4?

45

Dalla versione 4.11 è possibile specificare ordine di esecuzione utilizzando annotazioni e ordinamento per nome del metodo:

import org.junit.Test; 
import org.junit.FixMethodOrder; 
import org.junit.runners.MethodSorters; 

@FixMethodOrder(MethodSorters.NAME_ASCENDING) 
public class MyTest { 

    @Test 
    public void test1Create() { 
     System.out.println("first"); 
    } 

    @Test 
    public void test2Update() { 
     System.out.println("second"); 
    } 
} 

See JUnit 4.11 Release Notes

+0

Ottimo, grazie! – Hartmut

Problemi correlati