欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Config下的Spring Test几种方式实例详解

程序员文章站 2024-02-24 19:42:34
java config 下的spring test方式 用了三种方式: 1.纯手动取bean: package com.wang.test; i...

java config 下的spring test方式

用了三种方式:

1.纯手动取bean:

package com.wang.test;

import com.marsmother.commission.core.config.mapperconfig;
import com.marsmother.commission.core.config.propertyconfig;
import com.marsmother.commission.core.config.serviceconfig;
import com.marsmother.commission.core.dto.generalresponsedata;
import com.marsmother.commission.core.service.userservice;
import com.marsmother.commission.site.config.securityconfig;
import org.junit.before;
import org.junit.test;
import org.springframework.context.annotation.annotationconfigapplicationcontext;

/**
 * created by wanglei on 15/10/29.
 */
public class custometest {

  private static annotationconfigapplicationcontext context = new annotationconfigapplicationcontext();

  @before
  public void tearup(){
    context.register(propertyconfig.class);
    context.register(serviceconfig.class);
    context.register(securityconfig.class);
    context.register(mapperconfig.class);
    context.refresh();
  }

  @test
  public void testuser(){
    userservice userservice = context.getbean(userservice.class);
    long userid = 3l;
    generalresponsedata data = userservice.adduserrelation(userid);
    system.out.println(data.getmsg());
  }
}

2.采用spring-test

package com.wang.test;

import com.marsmother.commission.core.config.mapperconfig;
import com.marsmother.commission.core.config.propertyconfig;
import com.marsmother.commission.core.config.serviceconfig;
import com.marsmother.commission.core.dto.generalresponsedata;
import com.marsmother.commission.core.service.userservice;
import com.marsmother.commission.site.config.securityconfig;
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 wanglei on 15/10/29.
 */
@runwith(springjunit4classrunner.class)
@contextconfiguration(classes = {propertyconfig.class, serviceconfig.class, securityconfig.class, mapperconfig.class})
public class springtest {

  @autowired
  private userservice userservice;

  @test
  public void testuser(){
    generalresponsedata data= userservice.adduserrelation(3l);
    system.out.println(data.getmsg());
  }

}

3.采用mockito

需要引入相应包:

<dependency>
  <groupid>org.mockito</groupid>
  <artifactid>mockito-all</artifactid>
  <version>1.9.5</version>
  <scope>test</scope>
</dependency>
package com.wang.test;

import com.marsmother.commission.core.dto.generalresponsedata;
import com.marsmother.commission.core.presistence.follownumbermapper;
import com.marsmother.commission.core.presistence.usermapper;
import com.marsmother.commission.core.presistence.userrelationmapper;
import com.marsmother.commission.core.service.userservice;
import org.junit.before;
import org.junit.test;
import org.mockito.injectmocks;
import org.mockito.mock;
import org.mockito.mockitoannotations;

/**
 * created by wanglei on 15/10/29.
 */
public class testuserservice {

  @injectmocks
  private userservice userservice;

  @mock
  private follownumbermapper follownumbermapper;
  @mock
  private usermapper usermapper;
  @mock
  private userrelationmapper userrelationmapper;

  @before
  public void init(){
    mockitoannotations.initmocks(this);
  }

  @test
  public void testuser(){
    long userid = 3l;
    generalresponsedata result = userservice.adduserrelation(userid);
    system.out.println(result.getmsg());
  }

}

这里@mock的话,并不会真正的去执行数据库的操作。

还有一种用法是@spy,暂时不了解具体使用方式,待研究。

相比之下,还是spring-test标准一些。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!