私のssmフレームワーク統合-Spring-Testテスト単純クラス(1)


先に使用したパッケージはmaven管理で簡単にコピーしたり、自分でmaven公式サイトに行って探したりすればいいです.

<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.0modelVersion>

    <groupId>SpringTestgroupId>
    <artifactId>springTestartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>


        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>4.3.7.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.3.7.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>4.3.7.RELEASEversion>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.3.7.RELEASEversion>
        dependency>


        
        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-loggingartifactId>
            <version>1.1.1version>
        dependency>


        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>4.3.7.RELEASEversion>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>RELEASEversion>
        dependency>


    dependencies>

project>

Java Beanクラス、つまり自動アセンブリが必要なクラスのテストを開始します.
public class HelloWord {
    public void sayHelloWord(){
        System.out.println("Hello Word .....");
    }
}

間違えたみたいで大丈夫
次にSpringプロファイルを設定します

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloWorld" class="HelloWord" scope="singleton"/>
beans>

簡単です.つまり、プロファイルを使用してHelloWorldオブジェクトをhelloWorldと名付けて登録します.
まずJunitを使ってテストしますもちろんimportで対応するパッケージIdeaを簡単に追加しますもちろんこのステップは無視できます
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWordTest {
    @Test
    public void test(){
        HelloWord helloWord = new HelloWord();
        helloWord.sayHelloWord();
    }
    @Test
    public void testSpring(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWord word = (HelloWord) applicationContext.getBean("helloWorld");
        word.sayHelloWord();
    }


}

Javaクラスで定義したメソッドの結果が表示されます.テストに成功しました.
最後にSpringTestをテストするために必要なことは@RunWith@ContextConfiguration@Autowired@Test注記の使用です
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringTestHelloWord {
    @Autowired
    private ApplicationContext applicationContext;
    @Test
    public void test(){
        HelloWord helloWord =(HelloWord) applicationContext.getBean("helloWorld");
        helloWord.sayHelloWord();
    }
}

ContextConfiguration注記の後に付いているのは・SpringプロファイルのアドレスですちなみにオペレーティングシステムはMacがパスを変更するものです
テストを開始します.
Hello Word .....

テストに成功しました.