简体   繁体   English

如何从JUnit测试方法调用自动装配服务

[英]How to call autowired service from JUnit test method

I have a spring MVC small application and I need to integrate a test method using JUnit testing. 我有一个Spring MVC小型应用程序,需要集成使用JUnit测试的测试方法。

The class model I have is Page : 我拥有的课程模型是Page

@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name="page")
public class Page  implements Serializable {

    private static final long serialVersionUID = 1L; 

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name = "name")
    private String name;

    @Column(name = "about")
    private String about;

    @Column(name = "phone")
    private String phone;

    public int getId() {
        return id;
    }

    // the rest of getters and setters...
}

Also, I have a service which calls the PageDAO interface to connect to DB and get the data: 另外,我有一项服务,该服务调用PageDAO接口以连接到DB并获取数据:

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private PageDAO pageDao;

    public void saveOrUpdatePage(Page page) {
        pageDao.saveOrUpdate(page);
    }

    public Page getPage(int id) {
        Page page = pageDao.get(id);
        return page;
    }
}

PageDAOImpl : PageDAOImpl

@Repository("pageDAO")
public class PageDAOImpl implements PageDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public void saveOrUpdate(Page page) {
        sessionFactory.getCurrentSession().saveOrUpdate(page);
    }

    @Override
    @Transactional
    public Page get(int id) {
        String hql = "from Page where id=" + id;
        Query query = this.sessionFactory.openSession().createQuery(hql);

        @SuppressWarnings("unchecked")
        List<Page> listPage = (List<Page>) query.list();

        if (listPage != null && !listPage.isEmpty()) {
             return listPage.get(0);
        }
        return null;
    }
}

And finally, I have the test class PageControllerTest : 最后,我有测试类PageControllerTest

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("web.xml")
@ContextConfiguration
public class PageControllerTest {

    @Test
    public void testGetPageInfo() {
        Page expectedPage = new Page();
        expectedPage.setName("Pivotal");
        Page actualPage = new Page();
        // I need here to get a Page from the database using PageService `get` method passing 1 as page ID for parameter.
        assertEquals(expectedPage.getName(), actualPage.getName());
    }
}

How can I call the autowired PageService in order to get the page from db and compare it with the expectedPage name property? 我如何调用自动装配的PageService以便从db获取页面并将其与expectedPage name属性进行比较?

Thanks! 谢谢!

Try adding this annotation to the test class, 尝试将此注释添加到测试类中,

@ContextConfiguration("classpath:your-spring-beans-config-file-path.xml")

Autowire the service to @test class, 将服务自动连接到@test类,

@Autowired
PageService pageService;

You can inject dependencies in your test class, as you would in any controller, service or DAO class: 您可以像在任何控制器,服务或DAO类中那样在测试类中注入依赖项:

@Autowired
private PageService pageService;

There is a library called EasyMock .It provides a good and easy to integrate framework to write unit tests in a web application. 有一个名为EasyMock的库,它提供了一个易于集成的好框架,可以在Web应用程序中编写单元测试。

You know before hand what id's of the page might be(as part of your test data) and mock rest of the data in the model. 您事先知道页面的ID是什么(作为测试数据的一部分),并模拟了模型中的其余数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM