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})
它不會做任何改變 – Salman
您已刪除您的JUnit的依賴性:4.11? – glytching
謝謝。那麼'spring-boot-starter-test'包含'junit'?真正? – Salman