2017-02-06 3 views
0

У меня есть простой тестовый класс с использованием junit. К сожалению, он жалуется, когда я хочу запустить тестовый пример.Невозможно создать ApplicationContext с помощью NULL 'contextLoader'. Рассмотрите аннотацию тестового класса с @ContextConfiguration

Вот 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.springframewor[email protected]67b64c45] to prepare test instance a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration. 
    at org.springframework.util.Assert.notNull(Assert.java:115) 

ответ

0

Я считаю, @SpringApplicationConfiguration был устаревшим весенним ботинком 1.4.0.

Я не уверен, но может попытаться заменить @SpringApplicationConfiguration с @SpringBootTest например

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest 
public class RestUploaderApplicationTests { 

SpringBootTest автоматически настроить пружинный контекстную загрузчик (в то время как мы надеемся resloves ошибку: ... Экземпляр NULL «ContextLoader». Рассмотрите аннотацию своего тестового класса с @ContextConfiguration), а также автоматически настраивает MockMVC (хотя в этом случае вам это не понадобится)

Как в стороне, если это актуально план тестирования, он не похож на то, что ему нужно знать Spring, не существует каких-либо пружинных компонентов (@Beans/@MockBeans, @Autowired, @Controllers и т. д.), а обычный старый JUnit должен быть просто штраф

Надеется, что это помогает

EDIT // Добавленных источникам

@SpringApplicationConfiguration docs

@SpringBootTest docs

Смежные вопросы