这是我使用的文件:
component.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:component-scan
base-package="controllers,services,dao,org.springframework.jndi" />
</beans>
Run Code Online (Sandbox Code Playgroud)
ServiceImpl.java
@org.springframework.stereotype.Service
public class ServiceImpl implements MyService {
@Autowired
private MyDAO myDAO;
public void getData() {...}
}
Run Code Online (Sandbox Code Playgroud)
ServiceImplTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:conf/components.xml")
public class ServiceImplTest{
@Test
public void testMyFunction() {...}
}
Run Code Online (Sandbox Code Playgroud)
错误:
16:22:48.753 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2092dcdb] to prepare test instance [services.ServiceImplTest@9e1be92]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'services.ServiceImplTest': Injection of autowired dependencies failed; nested exception …Run Code Online (Sandbox Code Playgroud) 我正在做一个Spring Boot项目。我正在基于“ TDD”编写“单元测试”代码,这有点困难。
@SpringBootTest加载了所有的BEAN,这导致更长的测试时间。
因此,我使用了@SpringBootTest的类名称。
我已正常完成测试,但不确定使用@ContextConfiguration与使用@Import之间的区别。
这三个选项均正常运行。我想知道哪种选择是最好的。
@Service
public class CoffeeService {
private final CoffeeRepository coffeeRepository;
public CoffeeService(CoffeeRepository coffeeRepository) {
this.coffeeRepository = coffeeRepository;
}
public String getCoffee(String name){
return coffeeRepository.findByName(name);
}
}
public interface CoffeeRepository {
String findByName(String name);
}
@Repository
public class SimpleCoffeeRepository implements CoffeeRepository {
@Override
public String findByName(String name) {
return "mocha";
}
}
Option 1(SpringBootTest Annotation) - OK
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {CoffeeService.class, SimpleCoffeeRepository.class})
public class CoffeeServiceTest {
@Autowired
private CoffeeService coffeeService;
@Test
public void getCoffeeTest() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试我的Spring网络应用程序,但我遇到了一些问题.
我在我的maven中添加了一个测试类
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testName() throws Exception {
List<UserEntity> userEntities = userService.getAllUsers();
Assert.assertNotNull(userEntities);
}
}
Run Code Online (Sandbox Code Playgroud)
但是NullPointerException当我尝试运行此测试时,我得到了一个用户服务.IntelliJ说"无法自动装配.没有找到'UserService'类型的bean.添加后@RunWith(SpringJUnit4ClassRunner.class),我得到了这个例外
java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration
Run Code Online (Sandbox Code Playgroud)
我怎么解决呢?我想我需要在我的tomcat服务器上运行这个测试但是我如何使用IntelliJ进行测试?(比如命令'mvn clean install tomcat7:run-war-only')