Hi <@U04LA4NALVC> are you able to share the code? ...
# general
t
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
Hi @Tom, below method from Testbase
Copy code
@BeforeEach
public void initEach(){
  WireMockServer wireMockServer = new WireMockServer();
  wireMockServer.resetAll();
}
Copy code
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
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:
Copy code
stubFor(get("/bad")
  .withMetadata(metadata().attr("authStub", true))                        .willReturn(unauthorized()));
Then delete them via this metadata:
Copy code
removeEventsByStubMetadata(matchingJsonPath("$.authStub"));
l
Copy code
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
Can you elaborate exactly what you’d like to see and what’s actually happening?
l
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
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
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
Copy code
wiremock:
  reset-mappings-after-each-test: true
t
Are you able to share the actual code, in full?
l
For invalid
@Tom, do you need more? :)
t
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
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
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.