在我的spring boot
应用程序中,我有以下依赖项:
<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})
您的测试类应使用以下内容进行注释:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SeleniumApplication.class)
此 JUnit运行器需要 JUnit 版本>= 4.12。您对spring-boot-starter-test
的依赖将传递到所需的 JUnit 版本,以便您可以删除对 JUnit 的依赖。您对 JUnit 4.11 的明确依赖与 Spring 运行者的 JUnit 期望相冲突。