码迷,mamicode.com
首页 > 编程语言 > 详细

java的Spring学习2- junit

时间:2017-04-11 11:51:55      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:artifact   nap   hot   unit   import   autowired   build   print   snapshot   

1.maven依赖

 技术分享

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cnblog.gg</groupId>
    <artifactId>springTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.6.RELEASE</spring.version>
        <spring.junit>4.12</spring.junit>
        <springboot.version>1.5.1.RELEASE</springboot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${spring.junit}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2. java代码

package com.cnblog.gg;


public interface VedioPlayer {
    void play();
}

 

package com.cnblog.gg;

import org.springframework.stereotype.Component;
 

@Component("mp3")
public class Mp3Player implements VedioPlayer {
    @Override
    public void play()
    {
        System.out.println("I am mp3 player");
    }
}

 

3.spring扫描

 

package com.cnblog.gg;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@ComponentScan
public class VedioPlayerConfig {

}

 

4.单元测试代码

package com.cnblog.gg;

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;

import javax.annotation.Resource;

import static org.junit.Assert.*;

 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = VedioPlayerConfig.class)
public class Mp3PlayerTest {

    @Autowired
    private  VedioPlayer player;

    @Resource(name="mp3")
    private  VedioPlayer player2;

    @Test
    public void playNotNull()
    {
        assertNotNull(player);
        assertNotNull(player2);

        assertTrue (player==player2);
    }
}

 

java的Spring学习2- junit

标签:artifact   nap   hot   unit   import   autowired   build   print   snapshot   

原文地址:http://www.cnblogs.com/zhshlimi/p/6692382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!