Skip to content
정명주(myeongju.jung) edited this page May 9, 2018 · 3 revisions

Maven Execution

  • mvn clean test : only unit-test
  • mvn clean verify : unit-test + integration-test

pom.xml

    <properties>
        <spock.version>1.1-groovy-2.4</spock.version>
    </properties>

    <dependencies>
        <!-- test:spock -->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.spockframework</groupId>
                <artifactId>spock-bom</artifactId>
                <version>${spock.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    </dependencyManagement>
    <build>
        <plugins>
            <!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
            visit https://github.com/groovy/GMavenPlus/wiki -->
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compileTests</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useFile>false</useFile>
                    <excludes>
                        <exclude>**/*Spec.java</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <phase>integration-test</phase>
                        <configuration>
                            <excludes>
                                <exclude>none</exclude>
                            </excludes>
                            <includes>
                                <include>**/*Spec.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Directory Structure

  • src
    • main
      • java
      • resources
    • test
      • groovy : TestCase by Spock
      • java
      • resources

Unit Test

class WikiServiceTest extends Specification {
    WikiService service
    // mock
    WikiRepository wikiRepository
    WikiSettingService wikiSettingService

    // fixture
    Project project
    ProjectAdmin admin

    void setup() {
        wikiRepository = Mock()
        wikiSettingService = Mock()
        service = new WikiService(wikiRepository, wikiFactory, wikiSettingService)

        project = random(Project)
        admin = random(ProjectAdmin)
    }

    def "Create"() {    // dooray-위키/9
        given:
        def willCreateWiki = randomWiki(null)
        def createdWiki = random(Wiki)

        when:
        def result = service.create(project, admin)

        then:
        1 * wikiRepository.save(willCreateWiki) >> createdWiki
        and:
        result == createdWiki
    }
}

Integration Test

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
class WikiServiceSpec extends Specification {
    @Autowired
    WikiService wikiService

    def "Create"() {
        given:
        def project = random(Project)
        def admin = random(ProjectAdmin)

        when:
        def result = wikiService.create(project, admin)

        then:
        result.wikiId != null
        result.tenantId == project.tenantId
        result.projectId == project.projectId
        result.createOrganizationMemberId == admin.organizationMemberId
        result.createdAt != null
    }
}

With sonar

pom.xml

    <profiles>
        <!-- sonar -->
        <profile>
            <id>sonar</id>
            <properties>
                <sonar.tests>src/test/java,src/test/groovy</sonar.tests>
                <sonar.exclusions>'**/*Config.java','**/*Application.java','**/*Dto.java','**/model/Q**.java'</sonar.exclusions>
            </properties>
            <build>
                <plugins>
                    <!-- sonarqube -->
                    <plugin>
                        <groupId>org.sonarsource.scanner.maven</groupId>
                        <artifactId>sonar-maven-plugin</artifactId>
                        <version>3.4.0.905</version>
                    </plugin>
                    <!-- jacoco -->
                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.8.1</version>
                        <configuration>
                            <append>true</append>
                        </configuration>
                        <executions>
                            <execution>
                                <id>agent</id>
                                <goals>
                                    <goal>prepare-agent</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>jacoco-report</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>report</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
Clone this wiki locally