java spring单元测试失败,加载ApplicationContext失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45842345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java spring unit test failure , failed to load ApplicationContext
提问by Elias
I'm trying to do a unit test in my application to test a a method on service layer.
我正在尝试在我的应用程序中进行单元测试以测试服务层上的方法。
I have the following classes.
我有以下课程。
AbstractTest.java
抽象测试.java
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.equadis.webapp.controller.UserController;
//inform spring which class to use when executing unit test
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=UserController.class)
public abstract class AbstractTest {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
}
and
和
UserServiceTest.java that extends AbstractTest
扩展 AbstractTest 的 UserServiceTest.java
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.equadis.webapp.DAO.UserDAO;
import com.equadis.webapp.entity.UserVO;
import com.equadis.webapp.test.AbstractTest;
@Transactional
public class UserServiceTest extends AbstractTest {
@Autowired
private UserService service;
@Before
public void setup(){
service = new UserService();
}
@After
public void tearDown(){
//clean up after each test method
}
@Test
public void getListByID() {
UserVO user = service.getListByID(1);
System.out.println(user);
Assert.assertNotNull("failure - expected not null", user);
Assert.assertEquals("failure - expected id", 1, user.getId());
}
}
Noting that in Abstract.java class
注意到在 Abstract.java 类中
@SpringApplicationConfiguration(classes = Application.class)
was replaced by
被替换了
@SpringBootTest(classes=UserController.class)
So when i run a unit test i get the following errors:
因此,当我运行单元测试时,出现以下错误:
Stack trace:
堆栈跟踪:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:201)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalArgumentException: Can not build an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
at org.springframework.util.Assert.notNull(Assert.java:115)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:169)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:197)
... 17 more
Any help concerning this?
有任何帮助吗?
回答by techtabu
The classes
field in the @SpringBootTest
is the place you tell your application where your main Application Configuration is. It is not the place you specify your test classes. It should be like this,
中的classes
字段@SpringBootTest
是您告诉应用程序主要应用程序配置所在的位置。这不是您指定测试类的地方。应该是这样的
@SpringBootTest(classes=Application.class)
Besides, I think you are using old version of Spring boot. I think in 1.4/ 1.5 they moved Test class to,
此外,我认为您使用的是旧版本的 Spring Boot。我认为在 1.4/1.5 中,他们将 Test 类移到了,
@Runwith(SpringRunner.class)
回答by Elias
thanks everyone for your help , after reading all your comments and doing some research i've figured out the solution , with adding the following annotation:
感谢大家的帮助,在阅读了您的所有评论并进行了一些研究后,我已经找到了解决方案,并添加了以下注释:
@ContextConfiguration(locations={"/myxmlfile.xml"})
that contain the confiuration.
包含配置。