Я написал тестовые примеры JUnit, теперь я хочу запускать эти тестовые примеры JUnit в Docker-контейнере с помощью файла build.gradle, я совершенно новичок в 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 или еще что-нибудь?Спасибо.