In this tutorial we’ll reuse the code created in the stubs per consumer. We’ll write a test that instead of Stub Runner uses a JUnit rule to download and start stubs. Also we will start stubs on random port, retrieve that port and set it on the controller.

Important
This can be extremely useful when working with non-spring based applications on the consumer side!

The tests

  • Let’s open the consumer_with_stubs_per_consumer project

  • In that project we have a BeerControllerWithJUnitForBarTest - let’s open it

    • the test is ignored - let’s remove that line

    • as you can see the tests looks like BeerControllerForBarTest but it doesn’t use Stub Runner. If you run the test it will fail

  • Let’s add the missing JUnit Rule!

    @RegisterExtension static StubRunnerExtension rule = new StubRunnerExtension()
  • The StubRunnerRule is a fluent interface that allows you to set up the Stub Runner functionality. In our case we would like to (or copy from solutions)

    • download a stub with group id com.example and artifact id beer-api-producer-with-stubs-per-consumer

    • register it at port 8090

    • work offline

    • turn on the stub per consumer feature

    • define that the consumer’s name is bar-consumer

  • Now if you run the test it will pass! But let’s try to make the test better and randomize the port

  • First, remove the withPort line - we want the port to be randomly picked

  • The Rule has a method called findStubUrl - we can pass some coordinates to it to get the URL of a running stub. If we get the URL, we can get the port and pass it to the BeerController. Try it out (or copy from solutions)

  • And that’s it! You’ve successfully used the JUnit rule

Tip
You can check out the documentation for more information and you can also play with the API of the rule.
Important
To send messages via the rule you have to provide the MessageVerifier interface yourself. If you’re using Stub Runner, basing on the classpath we can set one for you. If you’re using a JUnit rule - you have to do that yourself.

Solutions

JUnit Rule

	@RegisterExtension static StubRunnerExtension rule = new StubRunnerExtension()
			.downloadStub("com.example","beer-api-producer-with-stubs-per-consumer")
			.stubsMode(StubRunnerProperties.StubsMode.LOCAL)
			.withStubPerConsumer(true)
			.withConsumerName("bar-consumer");

Port setting

	@BeforeEach
	public void setupPort() {
		this.beerController.port = rule.findStubUrl("beer-api-producer-with-stubs-per-consumer").getPort();
	}

Back to the main page