- application.properties
- application-dev.properties
- application-test.properties
- application-prod.properties
spring.profiles.active=test
util.count=0
util.total=4
util.count=1
util.count=2
util.count=3
util.count=2 util.total=4
- ForTestUtil (單純的載入兩個設定參數並提供印出的 method)
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class ForTestUtil { @Value("${util.count}") private Long count; @Value("${util.total}") private Long total; public void print() { System.out.println("count = " + count); System.out.println("total = " + total); } }
- ForTestUtilTest (上述類別的單元測試程式)
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import idv.steven.demo2.Demo2Application; @RunWith(SpringRunner.class) @SpringBootTest(classes = Demo2Application.class) public class ForTestUtilTest { @Autowired private ForTestUtil util; @Test public void test() { util.print(); } }
count = 2 total = 4
沒有留言:
張貼留言