SpringJunit4 ???е??????
???????????? ???????[ 2014/11/11 14:20:54 ] ??????????????? ???? ????
????????
????????WEB??????????д???????Dao??Service????????????????????????????????????Spring????????????????????У??????????ApplicationContext??????gerBean()???????в????????????????д????????????????????????????deBug????????????????Ч??????????Ч?????
??????????????spring???Junit4???е???????
???????μ??????????????????????????????????????
?????????????
????jar??????????????????????????
????????????????????????
?????????????test??·????λ??src?????£???????calsses??????£?????????·??????д
????????????????
????****************???????????????test???????************????????????????????****************
UserServiceTest
package test;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.spring.jpa.user.User;
import com.spring.jpa.user.UserService;
/** ?????????Spring??????? **/
@RunWith(SpringJUnit4ClassRunner.class)
/** ????spring?????????λ?????????????????λ?????????ж?????????????????????? **/
@ContextConfiguration(locations={"../spring-config/spring-jpa.xml"})
/** ?????????????????spring??????????????? **/
@Transactional
/** ??????????????????????????????????????????????????????????????????????? **/
@TransactionConfiguration(transactionManager="transactionManager"??defaultRollback=true)
public class UserServiceTest {
@Resource
private UserService userService;
@Test // ??????????20??????? ??????????????????????????????????
public void testSaveUser() {
for(int i=0; i<20; i++){
User user = new User();
user.setUserName("system");
user.setPassWord(i+"system");
userService.saveUser(user);
}
}
@Test // ??? ??????????????????????
public void testDeleteUser() {
userService.deleteUser(27L);
}
@Test // ???????
public void testFindAllUser() {
List<User> users = userService.findAllUsers();
System.out.println(users.size());
}
@Test // ??????????
public void testFindAllUserByPage() {
/**
* ?????????????? ?????0????????????5?????????С??????????????д??????????
* Direction?????????????????DESC??ASC???????
* id???????????id??????DESC????????
* ?????????????ü????????????????Щ????????
*/
PageRequest request = new PageRequest(1?? 4?? Direction.DESC?? "id");
Page<User> users = userService.findAllUserByPage(request);
// ??????????
System.out.println("??????????"+users.getTotalElements()+"????????????"+users.getSize()+"??????"+users.getTotalPages()+"????????"+(users.getNumber()+1)+"???");
// ??????????????
System.out.println(users.getContent());
}
// main ?????spring????bean??????????spring???????????????
public static void main(String[] args) {
// ????????????·????????????????·????????????·?????????-OUT???
String [] path = {"E:/moviework/springJpa/src/spring-config/spring-jpa.xml"};
ApplicationContext ac = new FileSystemXmlApplicationContext(path);
String[] beans = ac.getBeanDefinitionNames();
for(String s : beans) {
System.out.println(s); // ???bean??name
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11