Record method calls in one session for replaying in future test sessions?

前端 未结 9 1313
梦毁少年i
梦毁少年i 2020-12-23 14:21

I have a backend system which we use a third-party Java API to access from our own applications. I can access the system as a normal user along with other users, but I do no

9条回答
  •  醉酒成梦
    2020-12-23 14:32

    I had a similar problem some years ago. None of the above solutions would have worked for methods that are not pure functions (side effect free). The major task is in my opinion:

    • how to extract a snapshot of the recorded object(s) (not only restricted to objects implementing Serializable)
    • how to generate test code of a serialized representation in a readable way (not only restricted to beans, primitives and collections)

    So I had to go my own way - with testrecorder.

    For example, given:

    ResultObject b = callBackend(a);
    
    ...
    
    ResultObject callBackend(SourceObject source) {
      ...
    }
    

    you will only have to annotate the method like this:

    @Recorded
    ResultObject callBackend(SourceObject source) {
      ...
    }
    

    and start your application (the one that should be recorded) with the testrecorder agent. Testrecorder will manage all tasks for you, such as:

    • serializing arguments, results, state of this, exceptions (complete object graph!)
    • finding a readable representation for object construction and object matching
    • generating a test from the serialized data
    • you can extend recordings to global variables, input and output with annotations

    An example for the test will look like this:

    void testCallBackend() {
      //arrange
      SourceObject sourceObject1 = new SourceObject();
      sourceObject1.setState(...); // testrecorder can use setters but is not limited to them
      ... // setting up backend
      ... // setting up globals, mocking inputs
    
      //act
      ResultObject resultObject1 = backend.callBackend(sourceObject1);
    
      //assert
      assertThat(resultObject, new GenericMatcher() {
        ... // property matchers
      }.matching(ResultObject.class));
      ... // assertions on backend and sourceObject1 for potential side effects
      ... // assertions on outputs and globals
    }
    

提交回复
热议问题