当前位置: 首页 > 面试题库 >

如何用Spring Autowire编写JUnit测试?

壤驷乐邦
2023-03-14
问题内容

这是我使用的文件:

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>

ServiceImpl.java

@org.springframework.stereotype.Service
public class ServiceImpl implements MyService {

    @Autowired
    private MyDAO myDAO;

    public void getData() {...}    
}

ServiceImplTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:conf/components.xml")
public class ServiceImplTest{

    @Test
    public void testMyFunction() {...}
}

错误:

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 is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private services.ServiceImpl services.ServiceImplTest.publishedServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [services.ServiceImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ~[spring-beans.jar:3.1.2.RELEASE]

问题答案:

确保已导入正确的程序包。如果我正确地记住,有两种不同的自动布线套件。应该
org.springframework.beans.factory.annotation.Autowired;

这对我来说也很奇怪:

@ContextConfiguration("classpath*:conf/components.xml")

这是一个适合我的示例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext_mock.xml" })
public class OwnerIntegrationTest {

    @Autowired
    OwnerService ownerService;

    @Before
    public void setup() {

        ownerService.cleanList();

    }

    @Test
    public void testOwners() {

        Owner owner = new Owner("Bengt", "Karlsson", "Ankavägen 3");
        owner = ownerService.createOwner(owner);
        assertEquals("Check firstName : ", "Bengt", owner.getFirstName());
        assertTrue("Check that Id exist: ", owner.getId() > 0);

        owner.setLastName("Larsson");
        ownerService.updateOwner(owner);
        owner = ownerService.getOwner(owner.getId());
        assertEquals("Name is changed", "Larsson", owner.getLastName());

    }


 类似资料:
  • 以下是我使用的文件: 组成部分xml 服务我mpl.java est.java 错误:

  • 我正在尝试为这样的情况编写测试用例,在这个情况下,我期待的是datatruncation异常,我试图使用assert equals和比较消息来断言相同的情况,但是看起来像是比较两个字符串,有没有更好的方法来为这样的异常编写测试用例。 我正在使用JUnit5

  • 我是故意在谈论系统测试。我们确实有一套相当详尽的单元测试,其中一些使用了模拟,而这些测试不会去任何地方。系统测试应该是对单元测试的补充,因此,模拟不是一种选择。 如果我将替换为一个test-method(让我们称之为),并引入一个顺序依赖项(使用JUnit 5非常容易),它强制在运行任何其他测试之前运行,那么这些问题就会消失。 到目前为止还好!这看起来和工作很好。当测试不是由CI服务器执行,而是由

  • 我有一个程序,显示与名称和用户需要输入他们的字段。我怎么测试这个? AssertionError:JSON路径“$.FirstName”处没有值,异常:JSON不能为null或空 我的测试: storetest.java

  • 你好,我是java的新手,我对如何编写JUnit测试感到困惑,有人能帮忙吗? 任务说明: 当您将自动取款机(ATM)与银行卡一起使用时,您需要使用个人识别码(PIN)来访问您的帐户。如果用户在输入PIN码时失败三次以上,机器将阻止该卡。 假设用户的PIN是“1234”,编写一个程序,向用户索要PIN不超过三次,并执行以下操作: 如果用户输入了正确的号码,请打印一条消息,说明“您的PIN是正确的”,

  • customer-Mapper.xml daoimpl.java