Skip to content

Commit

Permalink
Merge pull request #20 from allegro/hermes-mock-without-rules
Browse files Browse the repository at this point in the history
Hermes mock without rules and with random port
  • Loading branch information
3750 authored Dec 18, 2023
2 parents 04a3c00 + 3305023 commit 3409b01
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
1 change: 0 additions & 1 deletion part2.3-message-broker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ dependencies {
testImplementation project(':commons')
testImplementation project(':commons-rest')
testImplementation 'pl.allegro.tech.hermes:hermes-mock:2.5.2'
testImplementation 'org.spockframework:spock-junit4:2.4-M1-groovy-4.0'
testImplementation 'org.spockframework:spock-spring:2.4-M1-groovy-4.0'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package pl.allegro.tech.workshops.testsparallelexecution.email.messagebroker

import org.junit.ClassRule
import org.spockframework.spring.EnableSharedInjection
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import pl.allegro.tech.hermes.mock.HermesMockRule
import org.springframework.context.annotation.Import
import pl.allegro.tech.hermes.mock.HermesMock
import pl.allegro.tech.workshops.testsparallelexecution.BaseTestWithRest
import spock.lang.Shared

Expand All @@ -12,14 +14,24 @@ import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
import static org.springframework.http.HttpStatus.OK
import static pl.allegro.tech.hermes.mock.exchange.Response.Builder.aResponse

class EmailsByMessageBrokerResourceTest extends BaseTestWithRest {
@EnableSharedInjection
@Import(HermesMockConfig)
class EmailsByMessageBrokerResourceTest extends BaseTestWithRest implements HermesMockPortSupport {

@Value('${application.services.message-broker.topic}')
private String topic

@ClassRule
@Shared
private HermesMockRule hermesMock = new HermesMockRule(8089)
@Autowired
private HermesMock hermesMock

def setupSpec() {
hermesMock.start()
}

def cleanupSpec() {
hermesMock.stop()
}

private String subject = "New workshops!"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.allegro.tech.workshops.testsparallelexecution.email.messagebroker

import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import pl.allegro.tech.hermes.mock.HermesMock

@TestConfiguration
class HermesMockConfig {
@Bean
HermesMock getHermesMock(@Value('${hermes-mock.port}') int hermesMockPort) {
return new HermesMock.Builder().withPort(hermesMockPort).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.allegro.tech.workshops.testsparallelexecution.email.messagebroker

import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.util.TestSocketUtils

trait HermesMockPortSupport {

@DynamicPropertySource
static void configurePort(DynamicPropertyRegistry registry) {
int port = TestSocketUtils.findAvailableTcpPort()
registry.add("hermes-mock.port", () -> port)
}
}
2 changes: 1 addition & 1 deletion part2.3-message-broker/src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
application.services.message-broker.url: http://localhost:8089
application.services.message-broker.url: http://localhost:${hermes-mock.port}
application.services.message-broker.topic: pl.allegro.tech.workshops.testsparallelexecution.email

0 comments on commit 3409b01

Please sign in to comment.