LoginSignup
0
0

More than 5 years have passed since last update.

JMockitを使ってプロパティファイルを開発用と本番用でスイッチをした

Last updated at Posted at 2017-08-31

初めてJMockitを触って色々と試したので備忘も兼ねて出来たことのメモ
・Java1.8
・EclipseOxygen(4.7.0)
・jmockit-1.33

テストクラス

import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.junit.Test;
import mockit.Invocation;
import mockit.Mock;
import mockit.MockUp;

public class PackagesTest {

    /**
     * 何もMockらない場合
     */
    @Test
    public void test_getData() {
        assertEquals("some_id"          ,Packages.getData("id"));
        assertEquals("some_password"    ,Packages.getData("password"));
    }

    /**
     * テスト用にプロパティファイルをスイッチする場合、loadかinputstreamの返り値をいじればよい?
     */
    @Test
    public void test_getData_MockPath() {
        new MockUp<Properties>() {
            @Mock
            public void load(Invocation inv,InputStream inputStream) {
                InputStream inputStream2;
                try {
                    inputStream2 = new FileInputStream("sample - コピー.properties");
                    inv.proceed(inputStream2);
                    inputStream2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        assertEquals("test_id"          ,Packages.getData("id"));
        assertEquals("test_password"    ,Packages.getData("password"));
    }

    /**
     * テストユニットのコード上にそのままリテラル値でMockUpした方がレビューの負担は少ない?
     */
    @Test
    public void test_getData_MockProperty() {
        new MockUp<Properties>() {
            @Mock
            public String getProperty(Invocation inv,String value) {
                switch (value){
                case "id":
                    return "mock_string";
                default:
                    return inv.proceed(value);
                }
            }
        };
        assertEquals("mock_string"      ,Packages.getData("id"));
        assertEquals("some_password"    ,Packages.getData("password"));
    }
}

メインクラス

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class Packages{

    public static void main(String[] args){
        System.out.println(getData("id"));
        System.out.println(getData("password"));
    }

    public static String getData(String arg) {
        String file = "sample.properties";
        String value = new String();
        Properties properties = new Properties();

        try {
            InputStream inputStream = new FileInputStream(file);
            properties.load(inputStream);
            inputStream.close();

            value = properties.getProperty(arg);

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return value;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0