2015-01-09 14 views
6

मेरे पास एक स्प्रिंग एप्लिकेशन है और इसमें नहीं है xml कॉन्फ़िगरेशन, केवल जावा कंडफिग का उपयोग करें। सब कुछ ठीक है, लेकिन जब मैं परीक्षण पर कोशिश करता हूं तो मुझे परीक्षणों में घटकों के अभाव को सक्षम करने में समस्याएं आ रही हैं। तो चलो मैं एक इंटरफ़ेसवसंत में परीक्षण के दौरान क्रूड रिपोजिटरी इंटरफ़ेस का उदाहरण कैसे बनाएं?

@Repository 
public interface ArticleRepository extends CrudRepository<Page, Long> { 
    Article findByLink(String name); 
    void delete(Page page); 
} 

और एक घटक/सेवा है शुरू करते हैं

@Service 
public class ArticleServiceImpl implements ArticleService { 
    @Autowired 
    private ArticleRepository articleRepository; 
... 
} 

मैं तो मेरे परीक्षण के लिए मैं ArticleServiceImpl केवल का उपयोग कर परीक्षण करने की कोशिश एक्सएमएल विन्यास का उपयोग नहीं करना चाहते हैं जावा कॉन्फ़िगरेशन। तो परीक्षण उद्देश्य के लिए मैंने बनाया:

@Configuration 
@ComponentScan(basePackages = {"com.example.core", "com.example.repository"}) 
public class PagesTestConfiguration { 


@Bean 
public ArticleRepository articleRepository() { 
     // (1) What to return ? 
} 

@Bean 
public ArticleServiceImpl articleServiceImpl() { 
    ArticleServiceImpl articleServiceImpl = new ArticleServiceImpl(); 
    articleServiceImpl.setArticleRepository(articleRepository()); 
    return articleServiceImpl; 
} 

}

में articleServiceImpl() मैं articleRepository() के कहने लगाने की जरूरत है, लेकिन यह एक अंतरफलक है। नए कीवर्ड के साथ नई वस्तु कैसे बनाएं? क्या यह एक्सएमएल कॉन्फ़िगरेशन क्लास बनाने और ऑटोवॉयरिंग सक्षम किए बिना संभव है? परीक्षण के दौरान केवल JavaConfigurations का उपयोग करते समय अवांछित सक्षम किया जा सकता है?

+0

नहीं, आप नहीं करते हैं। आपके पास '@ ऑटोवायर' है इसलिए आपको इसे सेट करने की आवश्यकता नहीं है। स्प्रिंग डेटा जेपीए आपके लिए बीन्स बनाने के लिए आपको अपनी कॉन्फ़िगरेशन क्लास पर '@ EnableJpaRepositories 'डालना होगा। ArticleServiceImpl के लिए –

+0

मेरे पास भी Awtowire है इसलिए मुझे ArticleServiceImpl() भी लिखने की आवश्यकता नहीं है? क्या मैं सही हू? मैं समझ नहीं पा रहा हूं कि वसंत परीक्षणों के लिए ऑटोवॉयरिंग को कैसे चालू करता है। नाम 'articleServiceImpl' नाम से बीन बनाने में त्रुटि: स्वत: निर्भर निर्भरताओं का इंजेक्शन विफल रहा; नेस्टेड अपवाद है org.springframework.beans.factory.BeanCreationException: क्षेत्र ऑटोवायर नहीं कर सका: निजी com.musala.repository.ArticleRepository – Xelian

+0

@ एम। डीनियम के पास सही जवाब है .. – Jaiwo99

उत्तर

10

यह मुझे मिला है जो मुझे वसंत नियंत्रक परीक्षण के लिए न्यूनतम सेटअप है, जिसे एक ऑटोवायर जेपीए रिपोजिटरी कॉन्फ़िगरेशन (एम्बेडेड वसंत 4.1.4.RELEASE, DbUnit 2.4.8 के साथ वसंत-बूट 1.2 का उपयोग करके) की आवश्यकता होती है।

परीक्षण एक एम्बेडेड एचएसक्यूएल डीबी के खिलाफ चलता है जो परीक्षण शुरू होने पर एक एक्सएमएल डेटा फ़ाइल द्वारा स्वत: पॉप्युलेट किया जाता है।

परीक्षण वर्ग:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = { TestController.class, 
            RepoFactory4Test.class }) 
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 
          DirtiesContextTestExecutionListener.class, 
          TransactionDbUnitTestExecutionListener.class }) 
@DatabaseSetup("classpath:FillTestData.xml") 
@DatabaseTearDown("classpath:DbClean.xml") 
public class ControllerWithRepositoryTest 
{ 
    @Autowired 
    private TestController myClassUnderTest; 

    @Test 
    public void test() 
    { 
     Iterable<EUser> list = myClassUnderTest.findAll(); 

     if (list == null || !list.iterator().hasNext()) 
     { 
      Assert.fail("No users found"); 
     } 
     else 
     { 
      for (EUser eUser : list) 
      { 
       System.out.println("Found user: " + eUser); 
      } 
     } 
    } 

    @Component 
    static class TestController 
    { 
     @Autowired 
     private UserRepository myUserRepo; 

     /** 
     * @return 
     */ 
     public Iterable<EUser> findAll() 
     { 
      return myUserRepo.findAll(); 
     } 
    } 
} 

नोट्स:

  • @ContextConfiguration एनोटेशन जो केवल एम्बेडेड TestController और जेपीए विन्यास वर्ग RepoFactory4Test भी शामिल है।

  • @TestExecutionListeners एनोटेशन क्रम में की जरूरत है कि बाद में एनोटेशन @DatabaseSetup और @DatabaseTearDown प्रभाव हो रहा है

संदर्भित विन्यास वर्ग:

@Configuration 
@EnableJpaRepositories(basePackageClasses = UserRepository.class) 
public class RepoFactory4Test 
{ 
    @Bean 
    public DataSource dataSource() 
    { 
     EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 
     return builder.setType(EmbeddedDatabaseType.HSQL).build(); 
    } 

    @Bean 
    public EntityManagerFactory entityManagerFactory() 
    { 
     HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     vendorAdapter.setGenerateDdl(true); 

     LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 
     factory.setJpaVendorAdapter(vendorAdapter); 
     factory.setPackagesToScan(EUser.class.getPackage().getName()); 
     factory.setDataSource(dataSource()); 
     factory.afterPropertiesSet(); 

     return factory.getObject(); 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager() 
    { 
     JpaTransactionManager txManager = new JpaTransactionManager(); 
     txManager.setEntityManagerFactory(entityManagerFactory()); 
     return txManager; 
    } 
} 

UserRepository एक सरल अंतरफलक है:

public interface UserRepository extends CrudRepository<EUser, Long> 
{ 
} 

EUse

@Entity 
@Table(name = "user") 
public class EUser 
{ 
    @Id 
    @Column(name = "id") 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Max(value=Integer.MAX_VALUE) 
    private Long myId; 

    @Column(name = "email") 
    @Size(max=64) 
    @NotNull 
    private String myEmail; 

    ... 
} 

FillTestData.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<dataset> 
    <user id="1" 
      email="[email protected]" 
      ... 
    /> 
</dataset> 

DbClean.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<dataset> 
    <user /> 
</dataset> 
+0

इस आलेख के लिए धन्यवाद। क्या आप कृपया 'RepoFactory4Test' कक्षा के वास्तविक लाभ को समझने में मेरी सहायता कर सकते हैं। – Sriram

+1

यूनिट टेस्ट क्लास में '@ कॉन्टेक्स्ट कॉन्फिगरेशन' एनोटेशन में घोषित दो बीन्स में से एक है। इसका मतलब है वसंत परीक्षण के चलाने के लिए केवल इन दो सेम स्वचालित रूप से लोड करता है। 'रेपोफैक्टरी 4 टेस्ट' स्वयं कुछ और बीन्स प्रदान करता है जिन्हें जेपीए के लिए काम करने की आवश्यकता होती है। इसके अलावा यह परिभाषित करता है कि कौन से भंडार का उपयोग किया जाना चाहिए (यहां '@ सक्षमजेपी रिपोजिटरीज') – Heri

+0

विवरण के लिए धन्यवाद। हालांकि, मेरी आवश्यकता में, मुझे '.xml' फ़ाइलों की बजाय' .sql' फ़ाइलों को लोड करना है (जैसा उपरोक्त उदाहरण में उल्लिखित है) और इस मामले में मैं उन्हें टेस्ट क्लास में लोड कर सकता हूं 'नियंत्रक WithRepositoryTest' कक्षा। अभी तक, मैंने उपरोक्त उदाहरण से 'dataSource()' विधि को 'addScript()' जोड़कर एसक्यूएल लोड किया है। लेकिन, एक सामान्य समाधान की तलाश में। कृपया सुझाव दे। – Sriram

0

आपको क्या करने की जरूरत है:

  1. ArticleRepository

  2. से हटाने @Repository आपके विन्यास कक्षा में @EnableJpaRepositoriesPagesTestConfiguration.java को

    @Configuration 
    @ComponentScan(basePackages = {"com.example.core"}) // are you sure you wanna scan all the packages? 
    @EnableJapRepository(basePackageClasses = ArticleRepository.class) // assuming you have all the spring data repo in the same package. 
    public class PagesTestConfiguration { 
    
    @Bean 
    public ArticleServiceImpl articleServiceImpl() { 
        ArticleServiceImpl articleServiceImpl = new ArticleServiceImpl(); 
        return articleServiceImpl; 
    } 
    } 
    
1

आप नहीं कर सकते उपयोग खजाने जोड़ने क्योंकि सी से ऑनफिगरेशन कक्षाओं में यह @EnableJpaRepositories का उपयोग करके अपने सभी भंडारों को पाता है।

  1. तो बदल अपने जावा विन्यास करने के लिए:
@Configuration 
@EnableWebMvc 
@EnableTransactionManagement 
@ComponentScan("com.example") 
@EnableJpaRepositories(basePackages={"com.example.jpa.repositories"})//Path of your CRUD repositories package 
@PropertySource("classpath:application.properties") 
public class JPAConfiguration { 
    //Includes jpaProperties(), jpaVendorAdapter(), transactionManager(), entityManagerFactory(), localContainerEntityManagerFactoryBean() 
    //and dataSource() 
} 
  1. आप तो
  2. नीचे की तरह एक अलग वर्ग बनाने के कई भंडार कार्यान्वयन वर्गों है, तो
@Service 
public class RepositoryImpl { 
    @Autowired 
    private UserRepositoryImpl userService; 
} 
  1. RepositoryImpl के लिए अपने नियंत्रक Autowire में और वहाँ से आप सभी को अपने भंडार कार्यान्वयन कक्षाएं पहुँच सकते हैं।
@Autowired 
RepositoryImpl repository; 

उपयोग:

repository.getUserService()।findUserByUserName (उपयोगकर्ता नाम);

ArticleRepository और ArticleServiceImpl में @ रिपोजिटरी एनोटेशन निकालें ArticleRepository को ArticleService पर लागू नहीं करना चाहिए।

2

आप स्प्रिंग बूट, आप कर सकते हैं उपयोग कर रहे हैं आर एक सरल @Entity एनोटेट वर्ग है अपने ApplicationContext में लोड करने के लिए @SpringBootTest जोड़ कर इन दृष्टिकोणों को थोड़ा सा सरल बनाएं। यह आपको अपने वसंत-डेटा भंडारों में स्वत: काम करने की अनुमति देता है। @RunWith(SpringRunner.class) तो वसंत विशेष एनोटेशन उठाया जाता है जोड़ना सुनिश्चित करें:

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class OrphanManagementTest { 

    @Autowired 
    private UserRepository userRepository; 

    @Test 
    public void saveTest() { 
    User user = new User("Tom"); 
    userRepository.save(user); 
    Assert.assertNotNull(userRepository.findOne("Tom")); 
    } 
} 

आप अपने docs में वसंत बूट में परीक्षण के बारे में और अधिक पढ़ सकते हैं।

संबंधित मुद्दे