gpt4 book ai didi

java spring单元测试失败,无法加载ApplicationContext

转载 作者:行者123 更新时间:2023-12-02 02:38:40 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中进行单元测试,以测试服务层上的方法。

我有以下类(class)。

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

对此有什么帮助吗?

最佳答案

classes @SpringBootTest 中的字段是您告诉应用程序主要应用程序配置所在的位置。它不是您指定测试类的地方。应该是这样的

@SpringBootTest(classes=Application.class)

此外,我认为您使用的是旧版本的 Spring boot。我认为在 1.4/1.5 中他们将测试类移至,

@Runwith(SpringRunner.class)

关于java spring单元测试失败,无法加载ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45842345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com