spring-integration-unit testing

POM

  • address
<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

  • java
/**
 * 配置spring和junit整合,junit启动时加载springIOC容器 spring-test,junit
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml"})// 告诉junit spring配置文件
//加上事物
  @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

  • Not adopted
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);
}
  • Not adopted
public class IUserServiceTest {

    public IUserService userService;
    @Test
    public void getUserByIdTest(){
        User user = userService.getUserById(1);
        System.out.println(user.getUserName());
    }
    @Before
    public void init() {
        // 创建spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        this.userService = context.getBean(IUserService.class);
    }
}
Published 20 original articles · Likes0 · Visits 930

Guess you like

Origin blog.csdn.net/vistaed/article/details/105558744