1

00:00:00.06 --> 00:00:02.01

- [Instructor] The final section

that we're going to talk about



2

00:00:02.01 --> 00:00:04.01

with Spring 5.0 in testing,



3

00:00:04.01 --> 00:00:05.08

is some of the new mock constructs



4

00:00:05.08 --> 00:00:07.06

and some of the improvements

that they have made



5

00:00:07.06 --> 00:00:10.01

to the existing mock construct.



6

00:00:10.01 --> 00:00:11.02

First thing we're going to talk about



7

00:00:11.02 --> 00:00:14.03

is MockHttpServletRequest.



8

00:00:14.03 --> 00:00:16.02

Now, there's some changes in the way



9

00:00:16.02 --> 00:00:19.00

that you can actually use

the servlet request mock.



10

00:00:19.00 --> 00:00:22.00

You can now get your

content as a byte array,



11

00:00:22.00 --> 00:00:23.04

or as a string.



12

00:00:23.04 --> 00:00:27.06

This access to raw content,

the request body for instance,



13

00:00:27.06 --> 00:00:29.00

really becomes a very powerful thing



14

00:00:29.00 --> 00:00:30.06

when you're inspecting elements,



15

00:00:30.06 --> 00:00:32.09

and I use it quite a bit with MockMvc,



16

00:00:32.09 --> 00:00:35.02

and having this here

from the servlet request,



17

00:00:35.02 --> 00:00:38.08

makes itself useful for

other tooling as well.



18

00:00:38.08 --> 00:00:41.03

Again, this is a small change,



19

00:00:41.03 --> 00:00:43.05

but it can be very useful if your testing



20

00:00:43.05 --> 00:00:45.08

can consume it, so if you're using



21

00:00:45.08 --> 00:00:48.00

the MockHttpServletRequest,

I encourage you



22

00:00:48.00 --> 00:00:50.08

to take a look at these two new options.



23

00:00:50.08 --> 00:00:52.07

Now MockMvc, which is

one of the frameworks



24

00:00:52.07 --> 00:00:54.05

that I use quite a bit,



25

00:00:54.05 --> 00:00:56.08

now has output, and I know that



26

00:00:56.08 --> 00:00:59.06

that may not seem like a

lot, but for me this is huge



27

00:00:59.06 --> 00:01:02.00

and in fact I'm going to

demo this here in a second,



28

00:01:02.00 --> 00:01:03.07

and the reason this is so big is



29

00:01:03.07 --> 00:01:07.00

when I'm poring through

lots of Jenkins logs,



30

00:01:07.00 --> 00:01:09.09

and I want to find out why my test failed,



31

00:01:09.09 --> 00:01:13.06

actually seeing the output

of the test is huge,



32

00:01:13.06 --> 00:01:16.08

especially in those use

cases of Jenkins files.



33

00:01:16.08 --> 00:01:18.08

I keep bringing that

up because I've used it



34

00:01:18.08 --> 00:01:21.06

considerably since Spring 5.0 came out



35

00:01:21.06 --> 00:01:25.00

because of the value

of having that output.



36

00:01:25.00 --> 00:01:27.03

So you get a print() and a log() support,



37

00:01:27.03 --> 00:01:29.07

you can use either one,

I usually just print it,



38

00:01:29.07 --> 00:01:31.05

because when I'm printing it,



39

00:01:31.05 --> 00:01:34.03

my log aggregator is going

to pick it up anyway.



40

00:01:34.03 --> 00:01:37.07

This visual information about

your test is very powerful,



41

00:01:37.07 --> 00:01:40.03

again in that CI/CD

world, and I don't want to



42

00:01:40.03 --> 00:01:42.04

underestimate how big of a deal this is



43

00:01:42.04 --> 00:01:45.00

even though it's a very small addition,



44

00:01:45.00 --> 00:01:46.04

to me this is one of the best things



45

00:01:46.04 --> 00:01:48.01

that was added in Spring 5.0,



46

00:01:48.01 --> 00:01:50.06

because it makes my life easier.



47

00:01:50.06 --> 00:01:52.09

So let's take a look at how to do that.



48

00:01:52.09 --> 00:01:55.01

So I'm going to go ahead and

jump over here to our IDE,



49

00:01:55.01 --> 00:01:57.06

and what I'd like you to

do is to open up the test



50

00:01:57.06 --> 00:02:01.03

that we created in the last video.



51

00:02:01.03 --> 00:02:02.05

The normal row you're

going to be looking at here



52

00:02:02.05 --> 00:02:04.08

is the actual output of our test,



53

00:02:04.08 --> 00:02:07.06

so let's run it as it exists today.



54

00:02:07.06 --> 00:02:10.03

So we'll run our test, and again it's



55

00:02:10.03 --> 00:02:13.04

the output that we're most interested in,



56

00:02:13.04 --> 00:02:16.06

we'll allow Spring to

start up and do its thing,



57

00:02:16.06 --> 00:02:19.02

and our test runs,



58

00:02:19.02 --> 00:02:20.07

and we get just a little bit of data



59

00:02:20.07 --> 00:02:24.08

that's a test that self-ran,

and then everything was good.



60

00:02:24.08 --> 00:02:27.09

So not a whole lot here to

give us much indication,



61

00:02:27.09 --> 00:02:31.02

but the test passed, it's

green, we're good to go, right?



62

00:02:31.02 --> 00:02:33.06

Well, sometimes I like a

little bit more information.



63

00:02:33.06 --> 00:02:35.05

So what we're going to do,



64

00:02:35.05 --> 00:02:38.07

is right before our Expect,



65

00:02:38.07 --> 00:02:41.00

we're going to add a

command and that command



66

00:02:41.00 --> 00:02:42.07

that we are going to add,



67

00:02:42.07 --> 00:02:47.01

is andDo, and we're simply

going to tell it to print.



68

00:02:47.01 --> 00:02:50.05

Now again, we could do

logging if we chose to,



69

00:02:50.05 --> 00:02:52.09

but in this case print will be sufficient.



70

00:02:52.09 --> 00:02:56.06

Now let's run our test again.



71

00:02:56.06 --> 00:02:59.01

And we'll see Spring doing

it's thing as it starts up



72

00:02:59.01 --> 00:03:01.04

and the context loads, the test context



73

00:03:01.04 --> 00:03:04.02

is running for our test,



74

00:03:04.02 --> 00:03:07.08

and there it is, so at the end here



75

00:03:07.08 --> 00:03:09.08

when the test actually was running,



76

00:03:09.08 --> 00:03:13.09

we now got a servlet request

and let's take a look at this.



77

00:03:13.09 --> 00:03:15.08

So the HTTP Method was POST,



78

00:03:15.08 --> 00:03:17.04

which is what we would expect.



79

00:03:17.04 --> 00:03:20.03

The URI was the context

root slash presidents,



80

00:03:20.03 --> 00:03:23.08

again what we would expect,

no parameters were sent,



81

00:03:23.08 --> 00:03:26.01

the content type is application json



82

00:03:26.01 --> 00:03:29.04

with a charset of UTF-8,

that's what we set



83

00:03:29.04 --> 00:03:31.03

so that's what we would expect to see.



84

00:03:31.03 --> 00:03:34.06

There's the json body

of our actual request,



85

00:03:34.06 --> 00:03:37.03

again, this information

becomes very valuable,



86

00:03:37.03 --> 00:03:39.01

if this was not a green test,



87

00:03:39.01 --> 00:03:42.00

we would know exactly what

data was sent to our servers,



88

00:03:42.00 --> 00:03:43.09

and we could then test it manually



89

00:03:43.09 --> 00:03:46.01

to determine what's breaking.



90

00:03:46.01 --> 00:03:47.08

As you go back down through you can see



91

00:03:47.08 --> 00:03:50.08

the method that was called on the handler,



92

00:03:50.08 --> 00:03:53.05

you can see whether it was

synchronous or asynchronous,



93

00:03:53.05 --> 00:03:55.07

in this case it was a synchronous test,



94

00:03:55.07 --> 00:03:57.07

you can look at all kinds of information



95

00:03:57.07 --> 00:03:59.08

including the output as well.



96

00:03:59.08 --> 00:04:02.03

So this information

becomes almost invaluable



97

00:04:02.03 --> 00:04:06.04

if you're troubleshooting

issues on your test execution,



98

00:04:06.04 --> 00:04:08.06

especially on those Jenkins boxes.



99

00:04:08.06 --> 00:04:12.08

So dig into this, I encourage

you if you're using MockMvc,



100

00:04:12.08 --> 00:04:15.02

add print to every

single one of your tests.



101

00:04:15.02 --> 00:04:18.01

You can probably write a

real quick script to do that,



102

00:04:18.01 --> 00:04:21.09

and you will find value out

of it immediately, however,



103

00:04:21.09 --> 00:04:24.02

take note that all of this

is going into your logs,



104

00:04:24.02 --> 00:04:27.00

so if you do retain your

logs for any period of time,



105

00:04:27.00 --> 00:04:29.07

it may cost you money in the long run,



106

00:04:29.07 --> 00:04:31.07

so take that into account that your logs



107

00:04:31.07 --> 00:04:33.04

are going to get bigger.



108

00:04:33.04 --> 00:04:35.06

It's a trade-off, as

everything in computer science



109

00:04:35.06 --> 00:04:37.05

but this one I think is very worth it.



