wild84 2017-11-07 09:09 采纳率: 80%
浏览 1188
已采纳

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

错误代码:

 No bean named 'attachementService' is defined

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

  • 写回答

3条回答 默认 最新

  • 独家de记忆 2017-11-07 10:07
    关注

    给你贴下我的代码吧

    通用父类

     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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料