juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Bognar <jamesbog...@apache.org>
Subject Fwd: Mock test task
Date Sat, 14 Dec 2019 20:31:56 GMT
One other useful note.  If you add debug="true" to the @Rest annotation on
the PetStoreResource class, the HTTP request/response will be printed on
the console which can be quite helpful in debugging issues.


---------- Forwarded message ---------
From: James Bognar <jamesbognar@apache.org>
Date: Sat, Dec 14, 2019 at 3:27 PM
Subject: Re: Mock test task
To: Rasa V <fiammara@gmail.com>
Cc: Ayeshmantha Perera <akayeshmantha@gmail.com>, <dev@juneau.apache.org>


Hi Rasa,

I have suggestions on your pull request.

You should be testing against the actual REST class itself instead of
creating pieces of it in your test (e.g. UpdatePetMockRest).  SpringRunner
allows you to get access to the real Spring bean via auto-wiring, like so:



*   @RunWith(SpringRunner.class)
 @ContextConfiguration(classes=App.class)*
*@SpringBootTest*

*public class PetStoreTest {   *   *@Autowired *
      *PetStoreResource petStoreResource;*

      *MockRest petStoreRest;*

      *@Before*
      *public void setup() {*
        * petStoreRest =
MockRest.create(petStoreResource).simpleJson().build();*
      *}*
   *}*

Then just use that petStoreRest object in all of your tests.

The MockRest class doesn't use Juneau serializers/parsers.  It just expects
you to work with raw string input and output.  So the following won't work
because it's just going to convert the CreatePet object to a String using
toString():

   *// Won't work*
   *petStoreRest.post("/petstore/pet",
pet).execute().assertStatus(200).assertBody(pet.toString());*

Instead, you'll want something like this:


*@Test*
*public void testCreatePet() throws Exception {  *   * petStoreRest*
       *  .post("/petstore/pet",
"{name:'Sunshine',price:100.0,species:'BIRD'}")*
            *.execute()*
          *  .assertStatus(200)*
           * .assertBody("1");  // Returns the auto-generated ID*

*}*

Note that you could use this approach if you defined this method on the
CreatePet class:

*public String toString() {  *   * return
SimpleJson.DEFAULT.toString(this);*
   *}*

But usually I just keep it simple and deal with just plain strings.

One last note:  Your test class should be placed in src/test/java.  This is
standard convention and allows your dependencies to be pulled in using only
test scope:



















*        <!-- Mock rest test -->        <dependency>
 <groupId>org.apache.juneau</groupId>
 <artifactId>juneau-rest-mock</artifactId>
 <version>8.1.2</version>           <scope>test</scope>
</dependency>        <dependency>           <groupId>junit</groupId>
   <artifactId>junit</artifactId>           <scope>test</scope>
</dependency>        <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>         </dependency>*

On Sat, Dec 14, 2019 at 1:51 AM Rasa V <fiammara@gmail.com> wrote:

> Pull request sent.
>
> 2019-12-14, št, 08:19 Ayeshmantha Perera <akayeshmantha@gmail.com> rašė:
>
>> Hi Rasa
>>
>> Send the PR after removing the commented mock tests.
>>
>> On Fri 13. Dec 2019 at 14:46, Rasa V <fiammara@gmail.com> wrote:
>>
>>> I dont`understand. Which mock test calls I should remove?
>>>
>>> 2019-12-13, pn, 12:28 Ayeshmantha Perera <akayeshmantha@gmail.com> rašė:
>>>
>>>> Hi Rasa.Send a PR remove all the commented code and I saw a dummy mock
>>>> test call ok remove it as well
>>>>
>>>> Great work done
>>>>
>>>>
>>>> On Fri 13. Dec 2019 at 11:21, Rasa V <fiammara@gmail.com> wrote:
>>>>
>>>>> I have pushed my code into a new branch:
>>>>> https://github.com/fiammara/juneau-petstore/tree/mock
>>>>> Posting pets doesn`t work, so this test also fails to run.
>>>>> You can check if i should correct something or I can do a pull request.
>>>>>
>>>>> 2019-12-13, pn, 12:01 Rasa V <fiammara@gmail.com> rašė:
>>>>>
>>>>>> Thanks. It works.
>>>>>>
>>>>>> 2019-12-12, kt, 17:40 James Bognar <jamesbognar@apache.org>
rašė:
>>>>>>
>>>>>>> If the REST class your testing is a Spring bean with injected
>>>>>>> fields, then using SpringRunner should work.  You get access
to the bean
>>>>>>> instance through autowiring.  So something like this....
>>>>>>>
>>>>>>> @RunWith(SpringRunner.class)
>>>>>>> @ContextConfiguration(classes = { App.class })
>>>>>>> @SpringBootTest()
>>>>>>> public class MyRestTest {
>>>>>>>
>>>>>>>     @Autowired
>>>>>>>     MyRestClass restClass;
>>>>>>>
>>>>>>>     @Test
>>>>>>>     public void myTest() throws Exception {
>>>>>>>         MockRest mr = MockRest.create(restClass);
>>>>>>>         mr.get("").execute().assertStatus(200);
>>>>>>>     }
>>>>>>> }
>>>>>>>
>>>>>>> On Thu, Dec 12, 2019 at 10:24 AM Rasa V <fiammara@gmail.com>
wrote:
>>>>>>>
>>>>>>>> Thank you. I have almost finished writing MockTest class.
 Which
>>>>>>>> annotation should I add for running tests? SpringRunner or
something else?
>>>>>>>>
>>>>>>>> 2019-12-12, kt, 16:56 James Bognar <jamesbognar@apache.org>
rašė:
>>>>>>>>
>>>>>>>>> HTTP GET requests do not typically have a request body.
 They're
>>>>>>>>> meant to pull data, not push.
>>>>>>>>>
>>>>>>>>> However, you can use the generic request() methods to
do it anyway:
>>>>>>>>> MockRest.request("GET", path, body)
>>>>>>>>>
>>>>>>>>> On Thu, Dec 12, 2019 at 1:13 AM Rasa V <fiammara@gmail.com>
wrote:
>>>>>>>>>
>>>>>>>>>> Thank you. I have one more question. How do you pass
@Body
>>>>>>>>>> parameters (an Object) into GET method? MockRest
Get has only one
>>>>>>>>>> parameter, the path.
>>>>>>>>>>
>>>>>>>>>> 2019-12-11, tr, 22:18 James Bognar <jamesbognar@apache.org>
rašė:
>>>>>>>>>>
>>>>>>>>>>> The annotation on your REST class should include
the following:
>>>>>>>>>>> @RestResource(serializers=JsonSerializer.class,
>>>>>>>>>>> parsers=JsonParser.class)
>>>>>>>>>>>
>>>>>>>>>>> On Wed, Dec 11, 2019 at 2:21 PM Rasa V <fiammara@gmail.com>
>>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>>> I tried to keep Json serializer annotation,
but I get an error
>>>>>>>>>>>> message saying that Json serializer cannot
be resolved as a type. Import
>>>>>>>>>>>> doesn`t help here.
>>>>>>>>>>>>
>>>>>>>>>>>> 2019-12-11, tr, 19:43 James Bognar <jamesbognar@apache.org>
>>>>>>>>>>>> rašė:
>>>>>>>>>>>>
>>>>>>>>>>>>> @RestResource-annotated methods can return
any POJO.  That
>>>>>>>>>>>>> POJO is converted to the body of the
HTTP response based on the Juneau
>>>>>>>>>>>>> Serializer that matches the Accept header
on the incoming request (e.g.
>>>>>>>>>>>>> "Accept: application/json")
>>>>>>>>>>>>>
>>>>>>>>>>>>> On Wed, Dec 11, 2019 at 6:29 AM Rasa
V <fiammara@gmail.com>
>>>>>>>>>>>>> wrote:
>>>>>>>>>>>>>
>>>>>>>>>>>>>> Hello. I have one question about
@Rest method. Should it
>>>>>>>>>>>>>> always return String or the type
is not important here?
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> 2019-12-11, tr, 09:06 Rasa V <fiammara@gmail.com>
rašė:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> I will try to fix the documentation
when I finish writing
>>>>>>>>>>>>>>> tests.
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>> 2019-12-10, an, 22:58 James Bognar
<jamesbognar@apache.org>
>>>>>>>>>>>>>>> rašė:
>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> Bonus points if you can verify
and fix that in the
>>>>>>>>>>>>>>>> documentation!  I see this
mistake in the Javadocs of the MockRest class.
>>>>>>>>>>>>>>>> The same mistake might exist
elsewhere.
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> On Tue, Dec 10, 2019 at 12:01
PM Ayeshmantha Perera <
>>>>>>>>>>>>>>>> akayeshmantha@gmail.com>
wrote:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Yes I think its already
there in the doc James pointed
>>>>>>>>>>>>>>>>> previously.
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> Thanks alot James
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>> On Tue 10. Dec 2019 at
17:57, James Bognar <
>>>>>>>>>>>>>>>>> jamesbognar@apache.org>
wrote:
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Ah.
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> Replace create(MyRest.class)
with build(MyRest.class).
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> On Tue, Dec 10, 2019
at 4:28 AM Ayeshmantha Perera <
>>>>>>>>>>>>>>>>>> akayeshmantha@gmail.com>
wrote:
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Hi Rasa and James
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> I just came back
from a business trip. Sorry to attend
>>>>>>>>>>>>>>>>>>> this lately.
James am attaching the screenshot and sending again
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> Regards
>>>>>>>>>>>>>>>>>>> Ayesh
>>>>>>>>>>>>>>>>>>> [image: Screenshot
from 2019-12-04 17-05-36.png]
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> On Wed, Dec 4,
2019 at 4:26 PM James Bognar <
>>>>>>>>>>>>>>>>>>> jamesbognar@apache.org>
wrote:
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> Hi Rasa,
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> Unfortunately
Apache mail does not support images so I
>>>>>>>>>>>>>>>>>>>> can't actually
>>>>>>>>>>>>>>>>>>>> see the error.
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> There's a
section in the documentation on the Mock REST
>>>>>>>>>>>>>>>>>>>> interface:
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> http://juneau.apache.org/site/apidocs-8.1.2/overview-summary.html#juneau-rest-mock
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> The general
idea behind them is to allow you to execute
>>>>>>>>>>>>>>>>>>>> simulated
HTTP
>>>>>>>>>>>>>>>>>>>> requests
against a REST endpoint without the need for a
>>>>>>>>>>>>>>>>>>>> servlet
>>>>>>>>>>>>>>>>>>>> container.
  I recommend trying out the examples in the
>>>>>>>>>>>>>>>>>>>> documentation
>>>>>>>>>>>>>>>>>>>> (a useful
exercise to validate the documentation as
>>>>>>>>>>>>>>>>>>>> well).
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>> On Wed, Dec
4, 2019 at 10:17 AM Rasa V <
>>>>>>>>>>>>>>>>>>>> fiammara@gmail.com>
wrote:
>>>>>>>>>>>>>>>>>>>> >
>>>>>>>>>>>>>>>>>>>> > Hello.
I just started writing a mock test. I don`h
>>>>>>>>>>>>>>>>>>>> have much
experience writing integration test. But I am trying to learn. I
>>>>>>>>>>>>>>>>>>>> wrote some
code and would like to be sure I am doing it correctly. I send
>>>>>>>>>>>>>>>>>>>> you a screenshot.
Here you can see that I am stuck with MockRest put
>>>>>>>>>>>>>>>>>>>> method. I
don`t know why do I get an error saying that The method
>>>>>>>>>>>>>>>>>>>> put(String,
UpdatePet) is undefined for the type MockRest.Builder.
>>>>>>>>>>>>>>>>>>>> > Maybe
do you have some good examples of Juneau mock
>>>>>>>>>>>>>>>>>>>> tests?
>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>> --
>>>>>>>>>>>>>>>>>>> *Software Engineer*
>>>>>>>>>>>>>>>>>>> *Salzburg Research
Forschungsgesellschaft *
>>>>>>>>>>>>>>>>>>> *Salzburg, Austria*
>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>> --
>>>>>>>>>>>>>>>>> *Software Engineer*
>>>>>>>>>>>>>>>>> *Salzburg Research Forschungsgesellschaft
*
>>>>>>>>>>>>>>>>> *Salzburg, Austria*
>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> --
>>>> *Software Engineer*
>>>> *Salzburg Research Forschungsgesellschaft *
>>>> *Salzburg, Austria*
>>>>
>>> --
>> *Software Engineer*
>> *Salzburg Research Forschungsgesellschaft *
>> *Salzburg, Austria*
>>
>

Mime
  • Unnamed multipart/related (inline, None, 0 bytes)
View raw message