SpringJunit 单元测试

来源:互联网 发布:淘宝开店教程下载 编辑:程序博客网 时间:2024/05/21 14:03



@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());
   
    }  

}

原创粉丝点击