JUnit Spring統合の例

JUnit + Spring統合の例

このチュートリアルでは、JUnitフレームワークを使用してSpring DIコンポーネントをテストする方法を示します。

使用される技術:

  1. JUnit 4.12

  2. ハムクレスト1.3

  3. Spring 4.3.0.RELEASE

  4. メーベン

1. プロジェクトの依存関係

SpringをJUnitと統合するには、spring-test.jarが必要です。

pom.xml

        
            junit
            junit
            4.12
            test
            
                
                    org.hamcrest
                    hamcrest-core
                
            
        
        
            org.hamcrest
            hamcrest-library
            1.3
            test
        
        
            org.springframework
            spring-context
            4.3.0.RELEASE
        
        
            org.springframework
            spring-test
            4.3.0.RELEASE
            test
        

2. ばね部品

後でテストするための単純なSpringコンポーネント。

2.1 An interface.

DataModelService.java

package com.example.examples.spring;

public interface DataModelService {

    boolean isValid(String input);

}

2.2 Implementation of above interface.

MachineLearningService.java

package com.example.examples.spring;

import org.springframework.stereotype.Service;

@Service("ml")
public class MachineLearningService implements DataModelService {

    @Override
    public boolean isValid(String input) {
        return true;
    }

}

2.2 A Spring configuration file, component scanning.

AppConfig.java

package com.example.examples.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = {"com.example.examples.spring"})
public class AppConfig {
}

3. JUnit + Spring統合の例

JUnitテストクラスに@RunWith(SpringJUnit4ClassRunner.class)アノテーションを付け、Spring構成ファイルを手動でロードします。 以下を参照してください。

MachineLearningTest.java

package com.example.spring;

import com.example.examples.spring.AppConfig;
import com.example.examples.spring.DataModelService;
import com.example.examples.spring.MachineLearningService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
public class MachineLearningTest {

    //DI
    @Autowired
    @Qualifier("ml")
    DataModelService ml;

    @Test
    public void test_ml_always_return_true() {

        //assert correct type/impl
        assertThat(ml, instanceOf(MachineLearningService.class));

        //assert true
        assertThat(ml.isValid(""), is(true));

    }
}

完了しました。

4. FAQs

4.1 For XML, try this :

import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations = {
        "classpath:pathTo/appConfig.xml",
        "classpath:pathTo/appConfig2.xml"})
public class MachineLearningTest {
//...
}

4.2 For multiple configuration files :

import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(classes = {AppConfig.class, AppConfig2.class})
public class MachineLearningTest {
//...
}