zoukankan      html  css  js  c++  java
  • 单元/集成测试 junit/testsng

    1、使用junit测试

    @RunWith(MockitoJUnitRunner.class) // 使用注解或者initMocks 注入mock public class TestJunit { @Mock TestDao testDao; @InjectMocks TestService testService; /* @Before public void initMock(){ MockitoAnnotations.initMocks(this); }*/ @Test public void test1() { when(testDao.Insert()).thenReturn("test"); String s = testService.insert(); System.out.println(s); } }


    2、使用junit集成springboot测试
    @SpringBootTest(classes = Springapplication22.class)    //springboot启动类,依靠启动类找扫描springcontext的bean
    @RunWith(SpringRunner.class)
    public class testJunitIntegration {

    @Autowired
    ApplicationContext context;

    @MockBean //从容器找到bean,并且把mockbean替换旧的bean
    TestDao testDao;

    @Autowired
    TestService te;


    @Test
    public void test1() {
    // TestService testService1 = (TestService)context.getBean(TestService.class);
    when(testDao.Insert()).thenReturn("test");
    // String insert = testService1.insert();

    String s = te.insert();
    System.out.println(s);
    }

    }
     
    3、使用Testng集成springboot测试
    @SpringBootTest(classes = Springapplication22.class)
    //@ContextConfiguration
    public class testngt2 extends AbstractTestNGSpringContextTests{

    @Autowired
    ApplicationContext context;

    @MockBean
    @Autowired //先从容器里取得bean,再mockbean替换
    TestDao testDao;

    @Autowired
    TestService testService;



    @Test
    public void test1() {
    // TestService testService1 = (TestService)context.getBean(TestService.class);
    when(testDao.Insert()).thenReturn("test");

    String insert1 = testService.insert();
    System.out.println(insert1);

    }

    }
     
  • 相关阅读:
    MySQL主从复制(7)回顾原理、企业场景部署
    MySQL主从复制(6)故障、快速配置
    *MySQL主从复制(5)实战
    MySQL主从复制(4)原理画图深入浅出
    MySQL主从复制(3)应用场景,切换不丢数据
    MySQL主从复制(2)介绍及分布式架构
    MySQL主从复制(1)DB各种同步方案
    找到小镇的法官
    最长回文子串
    权限管理
  • 原文地址:https://www.cnblogs.com/chenfx/p/14952204.html
Copyright © 2011-2022 走看看