我正在尝试在我的应用程序中进行单元测试,以测试服务层上的方法。
我有以下课程。
AbstractTest.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());
}
和
扩展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());
}
}
注意到在Abstract.java类
中@SpringApplicationConfiguration(classes = Application.class)
被
取代@SpringBootTest(classes=UserController.class)
因此,当我运行单元测试时,我会收到以下错误:
堆栈跟踪:
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$1.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
有关此事的任何帮助吗?
答案 0 :(得分:1)
classes
中的@SpringBootTest
字段是您告诉应用程序主应用程序配置所在的位置。它不是您指定测试类的位置。它应该是这样的,
@SpringBootTest(classes=Application.class)
此外,我认为您使用的是旧版Spring Boot。我认为在1.4 / 1.5中他们将Test类移到了
@Runwith(SpringRunner.class)
答案 1 :(得分:0)
感谢大家的帮助,在阅读了所有评论并做了一些研究之后我已经找到了解决方案,并添加了以下注释:
@ContextConfiguration(locations={"/myxmlfile.xml"})
包含配置。