Spring 测试

Posted ck_2016

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 测试相关的知识,希望对你有一定的参考价值。

1. pom.xml ==> Depency

<!-- Test -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <scope>test</scope>
</dependency>

<!-- Spring-beans -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
</dependency>

 

2. BaseTest.java

package com.ctrip.arch.titanqconfig;

import junit.framework.TestCase;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class BaseTest extends TestCase {


}

3. Sample

package com.ctrip.arch.titanqconfig.crypto;

import org.apache.logging.log4j.util.Strings;
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;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Soa2KeyService.class, DefaultDataSourceCrypto.class})
public class KeyServiceTester {
    @Autowired
    Soa2KeyService service;

    @Test
    public void testGetKey() throws Exception {
        String sslCode = "TT00000000000123";
        String key = service.getKeyInfo(sslCode).getKey();
        System.out.println("key=" + key);
        assert(Strings.isNotBlank(key));
    }
    
}

 

以上是关于Spring 测试的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

Spring boot:thymeleaf 没有正确渲染片段

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

CTS测试CtsWindowManagerDeviceTestCases模块的testShowWhenLockedImeActivityAndShowSoftInput测试fail项解决方法(代码片段

;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk