https://linen.dev logo
Title
t

Tom

02/06/2023, 11:20 AM
Hi @Leszek Izdebski are you able to share the code? As a general rule, when you’re driving WireMock programmatically from your tests I recommend doing a full reset before each test case (the rule/extension will do this for you if you use either). Then you build up the stubs you need at the start of each test case so that you have the exact stubs required for that test and nothing more.
l

Leszek Izdebski

02/06/2023, 11:27 AM
Hi @Tom, below method from Testbase
@BeforeEach
public void initEach(){
  WireMockServer wireMockServer = new WireMockServer();
  wireMockServer.resetAll();
}
public void mockAuthetication() {
  importStubs(
      stubImport()
          .stub(
              get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")))
          .stub(
              get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz}")))
          .stub(
              get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody(
                              "{ \"xyz" }")))
          .stub(
              post(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")))
          .stub(
              get(urlEqualTo(
                  "xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody(
                              "xyz")))
          .doNotDeleteExistingStubs());
}

public void mockInvalidAuthentication() {
  importStubs(
      stubImport()
          .stub(
              get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")))
          .stub(
              get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")))
          .stub(
              get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody(
                              "xyz")))
          .stub(
              post(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")))
          .stub(
              get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody(
                              "xyz")))
          .doNotDeleteExistingStubs());
}
t

Tom

02/06/2023, 11:29 AM
OK, I wouldn’t recommend using
stubImport()
this way. Calling
stubFor(…)
a number of times is more straightforward.
If you did want the option of selectively removing certain types of stub without deleting all you could tag them on creation like this:
stubFor(get("/bad")
  .withMetadata(metadata().attr("authStub", true))                        .willReturn(unauthorized()));
Then delete them via this metadata:
removeEventsByStubMetadata(matchingJsonPath("$.authStub"));
l

Leszek Izdebski

02/06/2023, 11:47 AM
public void mockAuthetication() {
  stubFor(get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(post(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(get(urlEqualTo("xyz"))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz]")));
}

public void mockInvalidAuthentication() {
  stubFor(get(urlEqualTo("xyz")).withMetadata(metadata().attr("authStub", true))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(get(urlEqualTo("xyz")).withMetadata(metadata().attr("authStub", true))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(get(urlEqualTo("xyz")).withMetadata(metadata().attr("authStub", true))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(post(urlEqualTo("xyz")).withMetadata(metadata().attr("authStub", true))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
  stubFor(get(urlEqualTo("xyz")).withMetadata(metadata().attr("authStub", true))
                  .willReturn(
                      ok().withHeader("Content-Type", "application/json")
                          .withHeader("Connection", "close")
                          .withBody("xyz")));
}
Now I have something like this and still doesn't work :(
t

Tom

02/06/2023, 11:49 AM
Can you elaborate exactly what you’d like to see and what’s actually happening?
l

Leszek Izdebski

02/06/2023, 12:03 PM
I have Class2, Class3 and everything works correctly. After adding Class1 I have few errors. For red icon: java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but no content-type was defined in the response. Try registering a default parser using: RestAssured.defaultParser(<parser type>); Why? When I started only this test it works correctly? For yelow icons: java.lang.AssertionError: 1 expectation failed. Expected status code <201> but was <401>.
t

Tom

02/06/2023, 12:16 PM
Hard to debug without seeing the exact code, but I definitely suggest moving your stubbing code out of your base class and into the specific test cases so that stubs are only created specifically for those tests. This will remove the possibility of stubs created for one test affecting others unintenionally.
l

Leszek Izdebski

02/06/2023, 12:20 PM
Yes, I did it. In every test I calling method with mock for example, in below case using correct mock
I don't use @BeforeAll, also I tried with below setting in my yml
wiremock:
  reset-mappings-after-each-test: true
t

Tom

02/06/2023, 12:27 PM
Are you able to share the actual code, in full?
l

Leszek Izdebski

02/06/2023, 12:33 PM
1.PNG
For invalid
@Tom, do you need more? :)
t

Tom

02/06/2023, 12:48 PM
Sorry, I can’t really tell what’s going on with this level of detail. Just to clarify - do you find that the test passes when you run it individually but fails when you run the whole suite?
l

Leszek Izdebski

02/06/2023, 12:56 PM
Each class and each test works correctly when I running only one test or one class. I always run test for all packages/suite. When I run Class2 and Class3 it works, after adding Class1 it failed.
t

Tom

02/06/2023, 1:10 PM
Usually when that happens it’s because some stubs are not being cleaned up between test cases/classes. It sounds like the reset call isn’t being made as expected. Two things to check: 1. Set a breakpoint on the line where you’re resetting and ensure it’s actually being called. 2. Set a breakpoint right at the start of your test case (before any stubs have been configured) then call
allStubMappings()
against the WireMock instance using the debugger. This should return an empty list, but if not the contents should provide a clue as to how it’s being populated.