Skip to content

Commit

Permalink
Remove DI
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldickson committed Sep 12, 2024
1 parent 689b8f2 commit fc91e0b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ replay_pid*
build/
out/
.metals/
.bloop/
.bloop/
.metals/metals.lock.db
.metals/metals.mv.db
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import scala.collection.mutable
import scala.util.{Failure, Success, Try}
import scalaj.http.HttpRequest


class TestMetricsReporter(httpClient: String => HttpRequest = Http.apply) extends Reporter {
class TestMetricsReporter extends Reporter {
private val objectMapper = new ObjectMapper().registerModule(DefaultScalaModule)
private val testCases = mutable.Map[String, TestCaseInfo]()
private var suiteStartTime: Instant = _
Expand All @@ -35,6 +34,8 @@ class TestMetricsReporter(httpClient: String => HttpRequest = Http.apply) extend
duration: Long = 0
)

protected def createHttpRequest(url: String): HttpRequest = Http(url)

override def apply(event: Event): Unit = event match {
case RunStarting(ordinal, testCount, configMap, formatter, location, payload, threadName, timeStamp) =>
suiteStartTime = Instant.now()
Expand Down Expand Up @@ -115,7 +116,7 @@ class TestMetricsReporter(httpClient: String => HttpRequest = Http.apply) extend
private def sendReportToEndpoint(jsonReport: ObjectNode): Unit = {
val jsonString = objectMapper.writeValueAsString(jsonReport)
Try {
val response = httpClient(endpointUrl)
val response = createHttpRequest(endpointUrl)
.postData(jsonString)
.header("Content-Type", "application/json")
.header("Charset", "UTF-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class TestMetricsReporterSpec extends AnyFunSpec with Matchers with MockitoSugar
when(mockResponse.isSuccess).thenReturn(true)

// Create a TestMetricsReporter with our mocked HTTP client
val reporter = new TestMetricsReporter(_ => mockRequest)
val reporter = new TestMetricsReporter {
override protected def createHttpRequest(url: String): HttpRequest = mockRequest
}

val runStartingEvent = RunStarting(ordinal = new Ordinal(1), testCount = 3, configMap = ConfigMap.empty, formatter = None, location = None, payload = None, threadName = "main", timeStamp = 123)
val testStartingEvent = TestStarting(ordinal = new Ordinal(2), suiteName = "TestSuite", suiteId = "suiteId", suiteClassName = Some("TestSuite"), testName = "test1", testText = "should do something", formatter = None, location = None, rerunner = None, payload = None, threadName = "main", timeStamp = 124)
Expand All @@ -40,7 +42,7 @@ class TestMetricsReporterSpec extends AnyFunSpec with Matchers with MockitoSugar
reporter(runCompletedEvent)

// Verify that HTTP request was made with correct data
verify(mockRequest).postData(argThat { (json: String) =>
verify(mockRequest).postData(argThat { json: String =>
val jsonNode = objectMapper.readTree(json)
jsonNode.get("totalTests").asInt() == 3 &&
jsonNode.get("succeededTests").asInt() == 1 &&
Expand Down

0 comments on commit fc91e0b

Please sign in to comment.