junitユニットテスト時にspring注入

3015 ワード

Springboot:原子サービスのテスト
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
@SuppressWarnings("deprecation")
public class RcSystemTest {
    @Autowired
    private AddRcSystemService addRcSystemService;
    @Test
    public void AddRcSystemTest() throws  Exception{
        RcSystemPo rcSystemPo=new RcSystemPo();
        rcSystemPo.setSysId((long) 2);
        rcSystemPo.setSysName(" ");
        int i = addRcSystemService.addRcSystem(rcSystemPo);
        System.out.println(i);
    }

    /**
     *    RcSystem
     * @throws Exception
     */
    @Test
    public void selectRcSystemTest() throws  Exception{
        RcSystemPO rcSystemPo=new RcSystemPO();
        rcSystemPo.setSysId(362338778643234816L);
        RcSystemPO rcSystemPO = rcSystemAtomService.selectRcSystem(rcSystemPo);
        Assert.assertEquals(" ",rcSystemPO.getSysName());
        Assert.assertEquals(" ",rcSystemPO.getSysAlias());
    }
}

コントロールをテストして、まずサービスを始めます.
import com.alibaba.fastjson.JSONObject;
import com.ohaotian.plugin.common.util.BeanMapper;
import com.ohaotian.plugin.db.Sequence;
import com.tydic.sz.RcSystem.bo.AddRcSystemReqBO;
import com.tydic.sz.RcSystem.service.AddRcSystemBusiService;
import com.tydic.sz.vo.AddRcSystemVo;
import lombok.extern.ohaotian.HTServiceRef;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
@SuppressWarnings("deprecation")
public class RcSystemControllerTest {
    @HTServiceRef
    private AddRcSystemBusiService addRcSystemBusiService;

    @Test
    public void AddRcSystemTest() throws  Exception{
        AddRcSystemVo addRcSystemVo=new AddRcSystemVo();
        // 
        Sequence sequence=Sequence.getInstance();

        addRcSystemVo.setSysId(sequence.nextId());
        addRcSystemVo.setSysName(" ");
        addRcSystemVo.setSysLevel(" ");
        addRcSystemVo.setSysOrg("xx ");
        addRcSystemVo.setSysUseObject(" ");
        addRcSystemVo.setNetEnvironment(" ");
        addRcSystemVo.setSysUrl("http://www.xxx.com");

        // 
        JSONObject jsonObject = (JSONObject) JSONObject.parse(addRcSystemVo.getExtend());
        //vo bo
        AddRcSystemReqBO addRcSystemReqBO=new AddRcSystemReqBO();
        BeanMapper.copy(addRcSystemVo, addRcSystemReqBO);
        addRcSystemBusiService.addRcSystem(addRcSystemReqBO, jsonObject);

    }
}


ssm:テストクラスの名前に注記を追加するには:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:beans.xml" }) @ContextConfiguration("classpath:beans.xml")