2013-06-06 9 views
7

Stumped. AspectJ sınıfını test etmeye çalışıyorum. Uygulamamı çalıştırdığımda Aspect sınıfım mükemmel bir şekilde yakalanır. Ancak, bir testte herhangi bir yöntemi engellemek için herhangi bir Aspect sınıfını alamıyorum gibi görünüyor. Ben burada İlkbahar 3.2.2, AspectJ 1.7.2 Maven'i 4.AspectJ önerisi, birim testi ile yapılmıyor

kullanıyorum

Ben ile çalışıyorum basit bir testtir:

Testi AspectJ'yi sınıfının

package my.package.path.config; 

import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

@Aspect 
public class TestAOP { 
    private String message; 

    public TestAOP() { 
    } 

    @Pointcut("execution(* my.package.path.TestAOPClient.relayMessage(..))") 
    public void aopPointcut() { 
    } 

    @Around("aopPointcut()") 
    public String monitor(ProceedingJoinPoint pjp) throws Throwable { 
     String msg = (String)pjp.proceed(); 
     this.setMessage(msg); 
     return msg; 
    } 

}

olan yöntem ele olan sınıf

package my.package.path.config; 

public class TestAOPClient { 
    public String relayMessage(String msg) { 
     return msg; 
    } 
} 

Testi Sınıf

package my.package.path.config; 

import static org.hamcrest.Matchers.equalTo; 
import static org.junit.Assert.assertThat; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 

@Configuration 
@ContextConfiguration(classes={WebConfig.class}) 
@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration("src/main/java") 
public class AopConfigTest extends AbstractJUnit4SpringContextTests { 

    @Bean 
    public TestAOP testAop() throws Exception { 
     return new TestAOP(); 
    } 

    @Test 
    public void assertTestConfigIsActive() { 
     TestAOPClient client = new TestAOPClient(); 
     client.relayMessage("hello"); 
     assertThat(((TestAOP)applicationContext.getBean("testAop")).getMessage(), equalTo("hello")); 

    } 
} 

WebConfig dosya Şaşmaz

package my.package.path.web.context; 
@Configuration 
@EnableWebMvc 
@EnableAspectJAutoProxy(proxyTargetClass=false) 
@ComponentScan(value={"my.package.path.config", "my.package.path.web"}) 
public class WebConfig { 

} 

, ben,

Expected: "hello" but: was null 

Benim WebApplicationContext zamanında beri alınmayı görünüyor iddia hatası alırsınız Aspect pointcutum için mevcut olmayan bir sınıfı belirtirseniz ApplicationContext hatası yüklenemedi.

Neyi eksik?

cevap

5

Ünite testini de @Configuration kaynağı olarak kullanmanız garip.

@Configuration ek açıklamasını birim testinden çıkarmalı ve testAOP() bakla tanımını WebConfig'a taşımalısınız. Ama en önemlisi fasulye el ile oluşturulan, ancak Bahar tarafından edilmemelidir tavsiye ediliyor:

@ContextConfiguration(classes={WebConfig.class}) 
@WebAppConfiguration("src/main/java") 
public class AopConfigTest extends AbstractJUnit4SpringContextTests { 

    @Autowired 
    private TestAOP testAop; 

    @Autowired 
    private TestAOPClient client; 

    @Test 
    public void assertTestConfigIsActive() { 
     client.relayMessage("hello"); 
     assertThat(((TestAOP)applicationContext.getBean("testAop")).getMessage(), 
      equalTo("hello")); 
    } 

} 
fasulye tanımıyla

Güncelleme yapılandırma:

@Configuration 
@EnableWebMvc 
@EnableAspectJAutoProxy(proxyTargetClass=false) 
@ComponentScan(value={"my.package.path.config", "my.package.path.web"}) 
public class WebConfig { 

    @Bean 
    public TestAOP testAop() throws Exception { 
     return new TestAOP(); 
    } 

    @Bean 
    public TestAOPClient testAopClient() throws Exception { 
     return new TestAOPClient(); 
    } 

} 

hedef ise AOP yapılandırmasına olup olmadığını test etmek çalışır ve TestAOP gerçekten bir test fasulyesidir (bu soru için sadece bir kukla isim değil), özel bir TestConfig konfigürasyon sınıfı oluşturabilir, fasulye tanımını oraya taşıyabilir ve @ContextConfiguration(classes={WebConfig.class,TestConfig.class}) testinden kullanabilirsiniz.

+1

Önerilenleri önerdiğiniz gibi taşıdım.Temiz görünüyor. Ama hala çalışmıyor. Şu anda, test sınıfının kodun src/main/java alanındaki her şeyim var, Test sınıfı ise src/test/java. TestAOP, WebConfig'te açıklanmıştır. Test çalıştırıldığında dokuma ne zaman ve nasıl gerçekleşir? Olması için bir yer ayarlamam gereken bir yapılandırma var mı? Ben Spring için çok yeniim. – Emilie

+1

Oh, sadece, test in TestAOPClient istemcisi = yeni TestAOPClient(); ... ... bu el ile testte hedef fasulye yarattığınızı fark ettiniz. Fasulye, fasulyeyi oluşturan kişi olmalı! Cevabımı güncelleyeceğim. –

+1

Yanıt güncellendi. –

1

Kişisel pointcut yapılandırma (paket test müşteri eşleşecek şekilde my.package.path.config edilmelidir)

@Pointcut("execution(* my.package.path.TestAOPClient.relayMessage(..))") 

yanlıştır ve müşteri bu

için

package my.package.path.config; 

public class TestAOPClient { 
... 

değiştirecektirler olduğunu

çalışmalıdır.

+1

Bu, şirketin adını ifşa etmemek için paket adımı değiştirdiğim bir yazım hatasıdır. Paketleri inceledim ve onlar doğru .. – Emilie

+0

Bu cevap için teşekkürler. Ben Eilie'ye benzer bir sorun yaşadım ve '@ Pointcut' ifadem,' Before Before 'tavsiyesinin çalışmasını engelleyen son bir sorundu! –