如何在春季自动测试课程?

时间:2015-06-15 22:18:22

标签: spring testing

我在春季为我的课程做了一些测试。

我正在制作模型和所有模型,但是我想要包含某些服务,我想知道是否有任何注释可以为测试用例创建@autowire

我检查了@contextConfiguration,但我不知道它是否合适。

由于

1 个答案:

答案 0 :(得分:1)

以下是您的测试类应如何显示的框架

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
@TransactionConfiguration(transactionManager = "<< YOURTRANSACTIONMANAGER >>", defaultRollback = true)
@Transactional
public class ServiceTest {

    //The name of your resource/service should be the same as defined in your bean definition
    @Resource
    private YourService service;

    @Test
    public void testYourService() {
        //Your Test code for eg.
        Object returned = service.doStuff();
        Assert.assertNotNull(returned);
    }


    public YourService getService() {
        return service;
    }


    public void setService(YourService service) {
        this.service = service;
    }

}

如需进一步阅读,请参阅此Spring unit testing