The first way: //spring integrates junit4 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Created by staunch on 2016-07-12. * version:v1.0 * instruction: initial version */ @RunWith(SpringJUnit4ClassRunner.class) // 整合 @ContextConfiguration(locations="classpath:config/applicationContext.xml") // load configuration public class AdvisorServiceTest { @Autowired private //Inject test dependencies @Test public void testCheckStudentInfo() throws Exception { // test code } } The second way: //Spring+testNG import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.testng.annotations.Test; import java.util.ArrayList; import java.util.List; /** * Created by staunch on 2016-06-29. * version:v1.0 * instruction: initial version */ @ContextConfiguration(locations = {"/config/applicationContext.xml"}) public class AdvisorMapperTest extends AbstractTestNGSpringContextTests{ @Autowired //Injected test dependencies @Test public void testBatchInsertDiscussion() throws Exception { // test code } ); }