1. 程式人生 > >SpringBoot工程Junit測試類案例

SpringBoot工程Junit測試類案例

@SpringBootTest
@RunWith(SpringRunner.class)
public class MongoDBTest2 {
	@Autowired
	MongoTemplate template;
	
//	@Test
	public void save() {
		List<Student> list = new ArrayList<>();
		for (int i = 0; i < 6; i++) {
			list.add(new Student("name"+i));
		}
		template.insert(list, Student.class);
	}
	
	@Test
	public void modifyById() {
		Criteria criteria = Criteria.where("_id").is(31);
		Query query = new Query(criteria);
		
		Update update = Update.update("name", "11111");
//		Student student = template.findOne(query, Student.class);
		Student student = template.findAndModify(query, update,FindAndModifyOptions.options().returnNew(true), Student.class);
		
		System.out.println("MongoAutoIncrementTest.modifyById()"+student);
	}
	
}

只要加入@SpringBootTest @RunWith(SpringRunner.class) 註解就能自動獲取所有bean