如何在Docker容器中使用build.gradle配置执行JUnit测试用例?

时间:2019-01-24 14:19:53

标签: java spring-boot docker build.gradle

我现在已经编写了JUnit测试用例,我想借助build.gradle文件在docker容器下运行这些JUnit测试用例,因为我是docker的新手,所以我不知道如何在以下环境下配置和运行JUnit测试用例码头工人 这是我的gradle文件 Build.gradle

buildscript {
    ext {
        springBootVersion = '1.5.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath('com.bmuschko:gradle-docker-plugin:3.0.8')
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8

task wrapper(type: Wrapper) {
    gradleVersion = '4.1'
}

jar {
    manifest {
        attributes 'Implementation-Title': 'ProjectName',
                   'Implementation-Version': '1.0'
    }
    archiveName = "project-name-api.jar"
}

apply plugin: 'org.springframework.boot'
bootRepackage {
    mainClass = 'com.xxx.xxx.app.TestApiApplication'
}

repositories {
    mavenCentral()
}

project.ext {
    springBootVersion = '1.5.6.RELEASE'
    dockerTag = "${System.env.DOCKER_TAG}"
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web-services')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('mysql:mysql-connector-java:5.1.6')
    compile('org.quartz-scheduler:quartz:2.2.1')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-security')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

apply plugin: 'com.bmuschko.docker-remote-api'
import com.bmuschko.gradle.docker.tasks.image.*

docker {
    url = 'https://18.20.80.49:1111'
    certPath = new File("${System.env.WORKSPACE}/certs")
    registryCredentials {
        url = 'some url'
        username = 'test'
        password = 'test'
        email = 'test@test.com'
    }
}

task createDockerfile(type: Dockerfile) {
    destFile = project.file('Dockerfile')
    instruction 'FROM ingensi/oracle-jdk:latest'
    instruction 'EXPOSE 80'
    instruction 'ENV java.net.useSystemProxies true'
    instruction 'ADD build/libs/project-name.jar /opt/'
    instruction 'ENTRYPOINT java -jar $JVM_MIN_MEM $JVM_MAX_MEM -Duser.timezone=UTC -Dhttp.proxyHost=$proxy -Dhttp.proxyPort=$proxy_port -Dhttps.proxyHost=$proxy -Dhttps.proxyPort=$proxy_port /opt/projectname.jar'
}

task buildImage( type: DockerBuildImage) {
    dependsOn jar
    dependsOn bootRepackage
    dependsOn createDockerfile
    inputDir = createDockerfile.destFile.parentFile
    tag = "path/project-name:$project.dockerTag"
}

task pushImage( type: DockerPushImage) {
    dependsOn buildImage
    imageName = "path/project-name"
    tag = "$project.dockerTag"
}

和我编写的这个示例测试类。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DepartmentService.class)
public class DepartmentServiceTest {
    @Autowired
    DepartmentService DepartmentService;

    @MockBean
    DepartmentRepository DepartmentRepository;

    @MockBean
    DepartmentAndUserHandler departmentAndUserHandler;
    Department ca = new Department();

    @Before
    public void initializeDepartment() {
        ca.setId(1);
        ca.setActive(true);
        ca.setSubId(1);
        ca.setCreated(new Timestamp(1l));
        ca.setLastUpdated(new Timestamp(1l));
        ca.setPassword("test");
        ca.setReplicateTimestamp(new Timestamp(1l));
        ca.setUserId(1);
        ca.setUserName("test");
        ca.setUserSecret("test");        
    }    

    @Test
    public void testGetDepartment() {

        Optional<Department> DepartmentOptional = Optional.of(ca);     

        Mockito.when(
                DepartmentRepository.findByDepartmentIdAndIsActive(Mockito.anyInt(), Mockito.anyBoolean()))
                .thenReturn(DepartmentOptional);
        Mockito.when(departmentAndUserHandler.createDepartmentAndUser(Mockito.anyInt())).thenReturn(ca);
        Mockito.when(DepartmentRepository.save(ca)).thenReturn(ca);

        Department caResponse = DepartmentService.getDepartment(1, 1l, true);
        assertThat(caResponse).isNotNull();
        assertThat(caResponse.getUserId()).isEqualTo(1l);
    }

}

我是否需要在gradle文件下进行一些更改? 谢谢。

0 个答案:

没有答案