POM
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
Junit test
- Guide package text package
Junit test (guide package text package ) |
|
@RunWith(SpringJUnit4ClassRunner.class) |
Automatically create spring container objects |
@ContextConfiguration(classes={T.class}) |
Specify that class as the configuration class |
@ContextConfiguration(locations=“classpath:*.xml”) |
Specify the path where the configuration file is located, relative to the path under the project |
@ContextConfiguration (locations = "file: absolute directory") |
Specify the path where the configuration file is located, which is an absolute path |
annotation
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml"})
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager")
public class IUserServiceTest {
@Autowired
public IUserService userService;
@Rollback(false)
@Test
public void getUserByIdTest(){
User user = userService.getUserById(1);
System.out.println(user.getUserName());
}
}
Way 2
public static void main(String[] args) {
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
OrdersMapper bean = (OrdersMapper)applicationContext.getBean( "ordersMapper" );
List<CustomerOrdersAdnUser> list = bean.findOrdersAndUser();
System.out.println(list);
}
public class IUserServiceTest {
public IUserService userService;
@Test
public void getUserByIdTest(){
User user = userService.getUserById(1);
System.out.println(user.getUserName());
}
@Before
public void init() {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
this.userService = context.getBean(IUserService.class);
}
}