Skip to content

Commit

Permalink
feat: Make complete packages mockable
Browse files Browse the repository at this point in the history
  • Loading branch information
and1x0 authored and joke committed Feb 7, 2022
1 parent 0338026 commit 0c17d8d
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ class PersonSpec extends Specification {
}
----

.Make complete package `io.github.joke` mockable
[source,groovy]
----
@Mockable(packages = "io.github.joke")
class PersonSpec extends Specification {
}
----

More examples can be found in link:examples[].

== How does it work
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.github.joke.spockmockable

import org.spockframework.mock.MockUtil
import spock.lang.Specification

@Mockable(packages = "io.github.joke.spockmockable")
class PersonTestWithMockablePackage extends Specification {

def mockUtil = new MockUtil()

def 'final from class is removed'() {
setup:
Person person = Mock()

expect:
mockUtil.isMock person
}

def 'final from method is removed'() {
setup:
Person person = Mock()

when:
def res = person.firstName

then:
1 * person.firstName >> 'Dorothy'

expect:
res == 'Dorothy'
}

def 'private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.lastName

then:
1 * person.lastName >> 'Gale'

expect:
res == 'Gale'
}

def 'final is removed and private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.address.street

then:
1 * person.address >> new Address('Yellow Brick Road', 'Blue City')

expect:
res == 'Yellow Brick Road'
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.joke.spockmockable

import org.spockframework.mock.MockUtil
import spock.lang.Specification

@Mockable(packages = "io.github.joke.spockmockable")
class PersonTestWithMockablePackage extends Specification {

def mockUtil = new MockUtil()

def 'final from class is removed'() {
setup:
Person person = Mock()

expect:
mockUtil.isMock person
}

def 'final from subclass is removed'() {
setup:
Person.Address address = Mock()

expect:
mockUtil.isMock address
}

def 'final from method is removed'() {
setup:
Person person = Mock()

when:
def res = person.firstName

then:
1 * person.firstName >> 'Dorothy'

expect:
res == 'Dorothy'
}

def 'private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.lastName

then:
1 * person.lastName >> 'Gale'

expect:
res == 'Gale'
}

def 'final is removed and private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.address.street

then:
1 * person.address >> new Person.Address('Yellow Brick Road')

expect:
res == 'Yellow Brick Road'
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.joke.spockmockable

import org.spockframework.mock.MockUtil
import spock.lang.Specification

@Mockable(packages = "io.github.joke.spockmockable")
class PersonTestWithMockablePackage extends Specification {

def mockUtil = new MockUtil()

def 'final from class is removed'() {
setup:
Person person = Mock()

expect:
mockUtil.isMock person
}

def 'final from subclass is removed'() {
setup:
Person.Address address = Mock()

expect:
mockUtil.isMock address
}

def 'final from method is removed'() {
setup:
Person person = Mock()

when:
def res = person.firstName

then:
1 * person.firstName >> 'Dorothy'

expect:
res == 'Dorothy'
}

def 'private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.lastName

then:
1 * person.lastName >> 'Gale'

expect:
res == 'Gale'
}

def 'final is removed and private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.address.street

then:
1 * person.address >> new Person.Address('Yellow Brick Road')

expect:
res == 'Yellow Brick Road'
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.joke.spockmockable

import org.spockframework.mock.MockUtil
import spock.lang.Specification

@Mockable(packages = "io.github.joke.spockmockable")
class PersonTestWithMockablePackage extends Specification {

def mockUtil = new MockUtil()

def 'final from class is removed'() {
setup:
Person person = Mock()

expect:
mockUtil.isMock person
}

def 'final from subclass is removed'() {
setup:
Person.Address address = Mock()

expect:
mockUtil.isMock address
}

def 'final from method is removed'() {
setup:
Person person = Mock()

when:
def res = person.firstName

then:
1 * person.firstName >> 'Dorothy'

expect:
res == 'Dorothy'
}

def 'private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.lastName

then:
1 * person.lastName >> 'Gale'

expect:
res == 'Gale'
}

def 'final is removed and private on method is now protected'() {
setup:
Person person = Mock()

when:
def res = person.address.street

then:
1 * person.address >> new Person.Address('Yellow Brick Road')

expect:
res == 'Yellow Brick Road'
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.joke.spockmockable

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get

import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.web.servlet.MockMvc
import spock.lang.Specification

@Mockable(packages = "io.github.joke.spockmockable")
@WebMvcTest(controllers = SimpleController)
class ControllerTestWithMockablePackage extends Specification {

@SpringBean
SimpleService simpleService = Mock()

@Autowired
MockMvc mockMvc

def 'final from class is removed'() {
when:
def response = mockMvc.perform(get('/name')).andReturn().response

then:
1 * simpleService.name >> 'This is a mock'

expect:
response.contentAsString == 'This is a mock'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import static org.codehaus.groovy.control.CompilePhase.OUTPUT
class MockableASTTransformation extends AbstractASTTransformation implements CompilationUnitAware {

private static Set<String> detectedClasses = [] as Set<String>
private static Set<String> detectedPackages = [] as Set<String>
private static CompilationUnit compilationUnit

@Override
void visit(ASTNode[] nodes, SourceUnit source) {
def annotation = nodes[0] as AnnotationNode
detectedClasses.addAll(getClassList(annotation, 'value')*.name as List)
detectedPackages.addAll(getMemberList(annotation, 'packages')?:[])
annotation.members.clear()
}

Expand All @@ -47,6 +49,7 @@ class MockableASTTransformation extends AbstractASTTransformation implements Com
propertyFile.withWriter("${UTF_8}", { writer ->
new Properties().with {
it['classes'] = MockableASTTransformation.detectedClasses.join(',')
it['packages'] = MockableASTTransformation.detectedPackages.join(',')
store(writer, 'Automatically generated by spock-mockable')
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
*/
Class<?>[] value() default {};

String[] packages() default {};
}
Loading

0 comments on commit 0c17d8d

Please sign in to comment.