SpringJunit 单元测试



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-hibernate.xml"})
public class TestUserService { 

@Autowired
IEmployeeAccountService employeeAccountService;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}


@AfterClass
public static void tearDownAfterClass() throws Exception {
}


// @Before
// public void setUp() throws Exception {
// ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] { "classpath:spring.xml", "classpath:spring-hibernate.xml" });
// employeeAccountService = (IEmployeeAccountService) ac.getBean("employeeAccountService");
//


@After
public void tearDown() throws Exception {
}


@Test
public void testUserLogin() {
assertTrue(employeeAccountService.employeeAccountLogin("Admin", "Admin"));
}
}



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:dispatcher-servlet.xml"})


public class UnitTestBeanInjection {

private static final Logger logger=Logger.getLogger(UnitDemoInjectionTest.class);


    // if no bean config in XMl, then you better declare the resource class with  @Repository annotation 
    @Autowired
    private UserService userserviceBean0;  
    
    // if do not want to use AutoWire, then you can use Resource annotation to find instance default by name
    @Resource
    private UserService userserviceBean;
    
    @Test  
    public void testUserService() {  
    assertNotNull(userserviceBean);
    }  
    
    @Test  
    public void testgetUserList() { 
//     assertNotNull(userserviceBean);
   
    System.out.println(userserviceBean.getUserList().size());
    }
    
    @Test  
    public void testaddUser() { 
    User u = new User();
    u.setName("JUnit");
    userserviceBean.addUser(u);
    System.out.println(userserviceBean.getUserList().size());
    assertEquals(4, userserviceBean.getUserList().size());
    }  
    


@Test  
    public void testshowUser() { 
    User u = new User();
    u = userserviceBean0.showUser("Tom");
    assertNotNull(u);
    System.out.println(u.getEmail());
   
    }  

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值