2016-04-08 11 views
0

TestExecutionListener hassas bir uygulama kullanmam gerekirse, varsayılan TestExecutionListeners yüklenmesini engeller. benim test sınıf yalnızca yüklü Dinleyici olacakBir TestExecutionListener Ekleme

@RunWith(SpringJUnit4ClassRunner.class) 
@TestExecutionListeners({MyCustomTestExecutionListener.class}) 
@ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" }) 
public class CabinetMembershipServiceImplTest { 
    ... 
} 

MyCustomTestExecutionListener gibidir ve benim testleri yürütülmesi başarısız yaparsa. Ben herhangi bir TestExecutionListener belirterek whitout benim testleri başlatmak ve ben Bahar 'günlüklerde kazmak zaman

, ben bulabilirsiniz:

getDefaultTestExecutionListenerClassNames : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]

benim TestExecutionListener eklemek istiyorsanız Yani, bir ek olarak (belirtmeniz gerekir istenen uygulama) benim test sınıfında tüm bu varsayılan TestExectionListeners:

@RunWith(SpringJUnit4ClassRunner.class) 
@TestExecutionListeners({ 
    ServletTestExecutionListener.class, 
    DirtiesContextBeforeModesTestExecutionListener.class, 
    DependencyInjectionTestExecutionListener.class, 
    DirtiesContextTestExecutionListener.class, 
    TransactionalTestExecutionListener.class, 
    SqlScriptsTestExecutionListener.class, 
    MyCustomTestExecutionListener.class}) 
@ContextConfiguration(locations = { "classpath:test-ApplicationContext.xml" }) 
public class CabinetMembershipServiceImplTest { 
    ... 
} 

sadece) TestExecutionListener birini (veya daha fazla ekleme bir yolu açıkça her Liste bildirmek zorunda whithout var mı varsayılan yapılandırmadan ne de varsayılan olanları "geçersiz kılar"?

cevap

2
import org.springframework.test.context.TestExecutionListeners.MergeMode; 

@TestExecutionListeners(value = { MyCustomTestExecutionListener.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS) 
1

açıkça varsayılan yapılandırmasından her dinleyici ilan etmek zorunda ne de varsayılan olanları "geçersiz kılma" whithout sadece bir (veya daha fazla) TestExecutionListener, ekleyerek bir yolu var mı?

Evet, bu bahar Framework 4.1 beri mevcuttur ve açıkça @TestExecutionListeners için Javadoc ve referans kılavuzun Merging TestExecutionListeners bölümündeki her iki belgelenmiştir. Aşağıdaki örnek doğrudan referans kılavuzundan alınmıştır.

@ContextConfiguration 
@TestExecutionListeners(
    listeners = MyCustomTestExecutionListener.class, 
    mergeMode = MERGE_WITH_DEFAULTS 
) 
public class MyTest { 
    // class body... 
} 

Selamlar,

Sam

(Bahar TestContext Çerçeve ait yazarı)