juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James Bognar <jamesbog...@apache.org>
Subject Re: Mock test task
Date Wed, 18 Dec 2019 14:56:59 GMT
Hi Rasa,

This is looking better.  I'm able to get 1 of the 17 tests to pass:
testPostOrder.  It appears the issue with the other tests appears to be
primarily because they're not independent of each other.  JUnit tests are
run in random order so you cannot rely on data created in one test to be
there for another.

For example, the testGettingPets() method below won't work unless
testPostPet() has been run first and there's no way to guarantee that in
JUnit:

@Test
public void testPostPet() throws Exception {
   petStoreRest
      .post("/pet", "{name:'Sunshine',tags:['nice'],
price:100.0,species:'BIRD'}")
      .execute()
      .assertStatus(200)
      .assertBody("1");
}

@Test
public void testGettingPets() throws Exception {
   petStoreRest
      .get("/pet")
      .execute()
      .assertStatus(200)

 .assertBody("[{id:1,species:'BIRD',name:'Sunshine',tags:['nice'],price:100.0,status:'AVAILABLE'}]");
}


As a general rule, testcases should not leave stuff around in the database
that can possibly interfere with other testscases.  So whatever you insert
should also be removed.  Here's an example of a test that inserts data,
performs your test, and then cleans up after itself:

@Test
public void testGettingOrders() throws Exception {

   // --- Initialize database with test data ---

   String petId = petStoreRest
      .post("/pet", "{name:'Sunshine',tags:['nice'],
price:100.0,species:'BIRD'}")
      .execute()
      .assertStatus(200)
      .getBodyAsString();

   String orderId = petStoreRest
      .post("/store/order", "petId=1&username=catlover")  // Has to be Form
Post format.
      .execute()
      .assertStatus(200)
      .getBodyAsString();

   // --- Test our method ---

   petStoreRest
      .get("/store/order/" + orderId)
      .execute()
      .assertStatus(200)

 .assertBody("{id:"+orderId+",petId:"+petId+",username:'catlover',status:'PLACED'}");

   // --- Delete our test data so that it doesn't interfere with other
tests ---

  petStoreRest
      .delete("/store/order/" + orderId)
      .execute()
      .assertStatus(200);

   petStoreRest
      .delete("/pet/" + petId)
      .execute()
      .assertStatus(200);
}

If you find yourself creating lots of duplicate code, then you can move
some of that logic into helper methods like so...

@Test
public void testGettingOrders() throws Exception {

   String petId = createTestPet();
   String orderId = createTestOrder();

   petStoreRest
      .get("/store/order/" + orderId)
      .execute()
      .assertStatus(200)

 .assertBody("{id:"+orderId+",petId:"+petId+",username:'catlover',status:'PLACED'}");

   deleteTestPet(petId);
   deleteTestOrder(orderId);
}

Hope that helps!





On Tue, Dec 17, 2019 at 2:32 PM Rasa V <fiammara@gmail.com> wrote:

> Thank you. I did one more commit to a Github. Could you check if my tests
> look better now?
>
> 2019-12-17, an, 18:49 James Bognar <jamesbognar@apache.org> rašė:
>
>> Hi Rasa,
>>
>> You'll want to put these tests under
>> juneau-petstore-server/src/test/java.  The juneau-petstore-api defines the
>> common beans used by both the server and client projects.  You're
>> specifically testing the server project so the tests should go in that
>> project
>>
>> Your test class shouldn't contain @Rest or @RestResource at all.  Instead
>> it should just be referencing PetStoreResource which has that annotation.
>>
>> If you're stuck, go ahead and submit a pull request with what you've got
>> and I can help out.
>>
>>
>> On Tue, Dec 17, 2019 at 6:58 AM Rasa V <fiammara@gmail.com> wrote:
>>
>>> I am stuck in many places. Almost all rest tests fail.
>>> What is more, I don`t know where to put @Rest or @RestResource
>>> annotation after removing static rest class.
>>> And where is the best place for the src/test/java catalogue:
>>> juneau-petstore-api or juneau-petstore-server? Do I need separate pom.xml
>>> for the tests?
>>>
>>> 2019-12-16, pr, 08:24 Rasa V <fiammara@gmail.com> rašė:
>>>
>>>> Thank you. I will try to fix everything.
>>>>
>>>> 2019-12-14, št, 22:32 James Bognar <jamesbognar@apache.org> rašė:
>>>>
>>>>> 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
View raw message