2017-08-10 124 views
1

在我的應用程序,我有以下的依存關係:單元測試未初始化

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <version>1.5.2.RELEASE</version> 
     <scope>test</scope> 
    </dependency> 

    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.11</version> 
     <scope>test</scope> 
    </dependency> 

我在下面的類中的方法,我需要測試:

@Service 
public class DataExtractorService { 

    @Autowired 
    LinksWriterService writer; 

    Kinkester kinkester; 

    public DataExtractorService(){ 
     kinkester=new Kinkester(); 
    } 

    public Kinkester extractor(String rowData){ 
      String pattern = "(\\d+)(\\D*)"; 

      Pattern r = Pattern.compile(pattern); 

      Matcher m = r.matcher(rowData); 
      if (m.find()) { 
      System.out.println("Found value: " + m.group(0)); 
      System.out.println("Found value: " + m.group(1)); 
      }else 
       System.out.println("NO MATCH"); 
      kinkester.setAge(Integer.parseInt(m.group(0))); 
      kinkester.setRole(m.group(1)); 

     return kinkester; 
    } 
} 

,然後測試等級是:

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = SeleniumApplication.class) 
@WebAppConfiguration 

public class DataExtractorTest { 


    @Autowired 
    DataExtractorService dataExtractorService; 

    @Test 
    public void extractor(){ 
     Kinkester kinkester = dataExtractorService.extractor("45M"); 
     System.out.println(kinkester.getAge()); 
     //assertEquals(kinkester.getAge(),45); 

    }  
} 

但不幸的是,測試不運行。它

Initialization Error (Runner: Junit 4) 

我曾嘗試下面的代碼抱怨,但仍然沒有答案了:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {SeleniumApplication.class}) 

回答

0

你的測試類應該被註釋:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes = SeleniumApplication.class) 

這JUnit運行,需要JUnit版本> = 4.12。您對spring-boot-starter-test依存度將在傳遞性帶來的JUnit所需的版本,所以你可以刪除JUnit的依賴。 JUnit的4.11你explcit依賴性與春季亞軍的JUnit的預期衝突。

+0

它不會做任何改變 – Salman

+0

您已刪除您的JUnit的依賴性:4.11? – glytching

+0

謝謝。那麼'spring-boot-starter-test'包含'junit'?真正? – Salman

相關問題