javaweb运行正常,junit却运行出错

错误代码:

 No bean named 'attachementService' is defined

用的是同一个配置文件,为何两处地方运行却截然不同?

给你贴下我的代码吧

通用父类

 import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-resources.xml"})
public class BaseJunit4Test {

}

调用业务类:

 import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import com.hshbic.selfservice.hardware.service.BaseJunit4Test;
import com.hshbic.selfservice.hardware.service.DeviceTemplateService;

public class DeviceTemplateServiceTest extends BaseJunit4Test{
    @Autowired
    private DeviceTemplateService deviceTemplateService;
    @Test
    @Transactional   //标明此方法需使用事务  
    @Rollback(true)  //标明使用完此方法后事务不回滚,true时为回滚  
    public void testUseTemplate(){
        deviceTemplateService.addDeviceTemplate(null);
    }
}

1、attachementService缺少注解导致没有创建对象
2、JUNIT测试类缺少相关注解导致加载时没有创建spring对象
3、问问题贴代码

得用框架的测试工具,不单单一个孤零零的junit
比如spring-test启动contenxt

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/app-db.xml"})