|
| 1 | +package com.baeldug.groovyconfig; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | + |
| 7 | +import org.junit.Test; |
| 8 | +import org.springframework.context.support.GenericGroovyApplicationContext; |
| 9 | + |
| 10 | +public class GroovyConfigurationTest { |
| 11 | + |
| 12 | + private static final String FILE_NAME = "GroovyBeanConfig.groovy"; |
| 13 | + private static final String FILE_PATH = "src/main/java/com/baeldug/groovyconfig/"; |
| 14 | + |
| 15 | + @Test |
| 16 | + public void whenGroovyConfig_thenCorrectPerson() throws Exception { |
| 17 | + |
| 18 | + GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(); |
| 19 | + ctx.load("file:" + getPathPart() + FILE_NAME); |
| 20 | + ctx.refresh(); |
| 21 | + |
| 22 | + JavaPersonBean j = ctx.getBean(JavaPersonBean.class); |
| 23 | + |
| 24 | + assertEquals("32", j.getAge()); |
| 25 | + assertEquals("blue", j.getEyesColor()); |
| 26 | + assertEquals("black", j.getHairColor()); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void whenGroovyConfig_thenCorrectListLength() throws Exception { |
| 31 | + |
| 32 | + GenericGroovyApplicationContext ctx = new GenericGroovyApplicationContext(); |
| 33 | + ctx.load("file:" + getPathPart() + FILE_NAME); |
| 34 | + ctx.refresh(); |
| 35 | + |
| 36 | + BandsBean bb = ctx.getBean(BandsBean.class); |
| 37 | + |
| 38 | + assertEquals(3, bb.getBandsList() |
| 39 | + .size()); |
| 40 | + } |
| 41 | + |
| 42 | + private String getPathPart() { |
| 43 | + String pathPart = new File(".").getAbsolutePath(); |
| 44 | + pathPart = pathPart.replace(".", ""); |
| 45 | + pathPart = pathPart.replace("\\", "/"); |
| 46 | + pathPart = pathPart + FILE_PATH; |
| 47 | + |
| 48 | + return pathPart; |
| 49 | + } |
| 50 | +} |
0 commit comments