SpringJunit 单元测试

218 阅读1分钟

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

}

}
---------------------
作者:xdy3008
来源:CSDN
原文:blog.csdn.net/xdy3008/art…
版权声明:本文为博主原创文章,转载请附上博文链接!