sizovs / PipelinR

PipelinR is a lightweight command processing pipeline ❍ ⇢ ❍ ⇢ ❍ for your Java awesome app.
https://github.com/sizovs/PipelinR
MIT License
420 stars 59 forks source link

Unit Test Examples #22

Closed zip-hashan closed 2 years ago

zip-hashan commented 2 years ago

Hi There,

Could you please include how to unit test the functionality in Kotlin/Java and Spring boot?

Cheers 🙂

sizovs commented 2 years ago

Hi @zip-hashan

Is there a specific testing challenge you're trying to overcome? All command/notification handlers are POJOs, so you can easily test them with your favorite unit testing and mocking library.

zip-hashan commented 2 years ago

Hi @sizovs, I am using MockK with JUnit and AssertJ on a Spring boot application. So, wondering how to mock and test Pipelinr handlers when using @MockK and @InjectMockKs annotations? 🤔

sizovs commented 2 years ago

@zip-hashan Just create an instance of Handler and provide mocked dependencies via constructor:

@Test
fun showsHowToTestHandlersWithMockk() {
  val dep1 = mockk<Dependency1>()
  val dep2 = mockk<Dependency2>()
  val handler = YourCommandHandler(dep1, dep2)
  var command = YourCommand()
  val result = handler.handle(command)
  assertNotNull(result)
}

In most of the unit testing/mocking libraries, you can also create mocks by annotating dependencies in your test. How to use MockK or other specific testing libraries is out of this project's scope. The best place is MockK documentation.