• java的Spring学习2- junit


    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);
        }
    }
  • 相关阅读:
    20190613 安装Anaconda
    20190613 一个SQL问题
    20190604 宠物引发的乱想
    windows下的vim打开文件乱码解决
    【题解】 「NOI2019」弹跳 KDT优化建图+最短路+剪枝+卡空间 LOJ3159
    【题解】 [AH/HNOI2017]单旋 LCT+splay
    【题解】 CF1400E Clear the Multiset 笛卡尔树+贪心+dp
    【题解】 CF1404C Fixed Point Removal 线段树+树状数组+带悔贪心
    【题解】 CF1251E2 Voting (Hard Version) 带悔贪心
    【题解】 CF1418G Three Occurrences hash+双指针+差分
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/6692382.html
Copyright © 2020-2023  润新知