我有一个使用junit的简单测试类。不幸的是,当我想运行测试用例时,它会抱怨。
这是maven:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.4.3.RELEASE</version>
<scope>test</scope>
</dependency>然后在测试类中是:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RestUploaderApplication.class)
public class RestUploaderApplicationTests {
Station station1;
Station station2;
Content content1;
Content content2;
ContentRepository contentRepo;
StationRepository stationRepo;
@Before
public void createObjects(){
Station station1=new Station("UT","Livii 2");
Station station2=new Station("City Center","Kissing Square");
Content content1=new Content(station1,"BMW","Text","google.com",10,true);
Content content2=new Content(station2,"SWB","Image","swb.com",100,true);
}
@Test
public void insertInstancesTest() {
int size=station1.getContents().size();
assertEquals(1,size);
}}最后,在运行时出现错误:
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@67b64c45] to prepare test instance a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
at org.springframework.util.Assert.notNull(Assert.java:115)发布于 2017-02-07 04:12:28
我相信@SpringApplicationConfiguration在SpringBoot1.4.0中被否决了。
我不确定,但可以试着用@SpringBootTest代替@SpringBootTest吗
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RestUploaderApplicationTests {SpringBootTest应该自动配置春季上下文加载器(同时希望重新设置错误:...instance a NULL 'contextLoader‘。考虑用@ContextConfiguration注释您的测试类),并自动配置您的MockMVC (虽然在本例中您似乎不需要)
顺便说一句,如果这是实际的测试计划,它看起来不需要被Spring感知,似乎没有任何使用的spring组件,(@Beans / @MockBeans,@Autowired,@ JUnit等等)和常规的旧JUnit应该是很好的
希望这能有所帮助
编辑//添加源
@SpringApplicationConfiguration docs
@SpringBootTest文档
https://stackoverflow.com/questions/42078027
复制相似问题