Flux - Learn to write JUNIT Tests
A free video tutorial from Pragmatic Code School
Technology Enthusiast, Online Instructor
4.5 instructor rating • 7 courses • 39,854 students
In this tutorial we will learn about how to write JUNITs for the Flux reactive type.
Learn more from the full courseBuild Reactive RESTFUL APIs using Spring Boot/WebFlux
Learn to write reactive programming in Spring using WebFlux/Reactor and build Reactive RESTFUL APIs.
09:38:13 of on-demand video • Updated June 2021
- What problems Reactive Programming is trying to solve ?
- What is Reactive Programming?
- Reactive Programming using Project Reactor
- Learn to Write Reactive programming code with DB
- Learn to Write Reactive Programming with Spring
- Build a Reactive API from Scratch
- Learn to build Non-Blocking clients using WebClient
- Write end to end Automated test cases using JUNIT for the Reactive API
English [Auto] Hi, everyone, welcome back to the territorial initiatory learn about the techniques of writing the task cases that is going to assert on the elements that are part of the flux. I'm going to open the diskless flux on Monotheist when I zoom in a little bit and then let's go ahead and write the Friskies. I'm going to name this one as Public Llyod. Luks. Dust flux. Just elements and then without error, so I'm not going to have any error as part of the flux, so what I'm going to do, I'm going to just copy this first line. And then put it here, followed by that, I'm going to add the lock. So we have the luxury ride. The next thing is we are going to test the elements inside the flux, which are spring springboard and react to spring, and then the order in which the elements are flowing to the subscriber right now in order to test the elements. Reactor test is a module that we are going to use. It has a class called Step Verifier. Let's create an instance of it. All right. I mean, it's called the glass and then it has a method. Colquitt. All right, for this matter, we are going to pass the flux. OK, that's good, and the next step is what is it we are expecting, let's go home, run this. I'm going to have. I want to hold on those and then run this and then check what is that we are printing? So this is a test which we wrote in the previous video in here, if you take a look at the possible spring, the next value, a springboard, and the next rally was a reactive spring. So that's what we have in our flux. Right. So. The step for your House Democrats like Dodd expect next, so in the expect next you can provide. Spring, that's the first rally that we expect, right? So that's supposed to value the flux, is going to add to the subscriber. And then the next thing is what is the next value it's going to? Well, in many ways, this or that, the court will be clear. Let me put it like this, there you go, and then the next thing is it's going to be spring board. So I'm going to put the put the spring wood here. Right, let's put that up because. And followed by that, what are the next world you expect next? And the next value's reactor spring, so I'm going to put that value also here. So let me formatters. There you go. Now, what we will do, we will try to run the justice and then what is the last year? And if you take a look at it, since there is no error, we are going to have to verify complete, right. That's the last even we call the very complete. That's when the actual data is going to flow. OK, now let's go ahead and run this. Particularly worried that this case passed and then all the events are flowing as expected, just for the sake of making sure this works as expected. What I'm going to do, I'm going to change the order and then see what happens. So when we change the order, the expectation is that we want to make sure this test fails. Let's go out and run it. There you go. The test case failed because we change the order, the expectation as a springboard, but it failed with the value spring. So this just goes make sure. We are getting the elements from the flux in the order that we have it here. So the difference between these two is that here we call the subscriber to write the subscriber method is one which is going to compete, which is going to start the whole flow of the flux to elements from the flux to the subscriber. But in here, if you take a look at it, the step verifier takes care of subscribing to the flux and then asserting the values on the flux. So does a very complete if we do not call what happens? What I'm going to do is I'm going to come on this and then show you what happens if we do not make a call to verify a complete what happens. Let's go ahead and run this. You see here, right? We do not have any even's of flowing from Cluck's, the reason being very, very complete, a call which is actually equivalent to subscribe. That's a one which is going to actually start the flow of elements from the Fluxus. So you have to make sure always you end up the Cascades with the verified call. Right now, let's go out and explore the next method, which is. Flux, just elements error, so I'm going to put with error and what I'm going to do is I'm going to use the concat with which we have here. Let's copy this. And then put it here, so the carpet is going to add the exception, right, and then here we are actually expecting the verified complete as a last event. But that's not the case here because verifier error is going to be the last event because we have the expecta exception added to our flux. Let's go out and run this. Basically, this just is going to feel. But still, I want to show you what happens when you're on it so the expectation is complete. Let me zoom in a little bit. The expectation is expect complete, but the actual actually on error. Right. So how do we write test cases so that we can assert on the exception? So it does a handy method called expect so to not expect error. So if you take a look at it, expect error and then you can give runtime exceptional class. So here the exception type RSE runtime exception. That's the reason why I'm putting the expected error and the type of the exception as the argument toward the very complete won't work because we're complete doesn't make sense here. We are expecting the error rate. So in that case, the methods that are available to us is just really fine. Let's go out and call this verify, because verify is a one which is going to actually start the whole flow of flux from the flux to the subscriber. So here are the step verified as a subscriber. Right. Let's go ahead and run this diskettes. You run the test case, there you go. It gave you the green signal, meaning that this case is working as expected. Let's say you want to just verify the message inside the exception. So in that case, there is 100 method also available for that. So we're going to do expect. Error message, Dirigo error message, and then. We can pass the string as an input to that error message. And then let's go around on this. There you go and gave it the green signal, meaning that this case worked as expected, if you. Can we have both of these? The answer is no, you cannot have them both together. Let's go ahead and run this one more time. There you go. So now let's say you don't want to validate all the values, you just want to validate the number of elements that particular flux is going to emit. So in that case, what we will do so I'm going to create another matter. I want to copy this whole method. And then put it here, so I'm going to name this one as let's minimize this. Flux elements. Flux elements count. So you just want to know also on the number of elements, right? So the step forward has a handy method for that called expect next. So in the county of El, the value US three. And then followed by that hospital use case, we have a exception, that's the reason why I have the expected error message to be exceptionally good. If you run this, that's going to work as expected. There we go and I'm going to show you one more variation here flusters. With error only. So Flux just with Erev. So here we have I'm going to name this one here we have expected next as separate lines, right? You don't have to do what you can do us. You can just put everything in a single line to like this and followed by that. I can do the reactor's spring. What I'm going to do, I'm going to remove these lines. And I'm going to run this Barondess, it's going to give me the green signal. The reason being this is one of the other way of asserting on the elements in the flux. So these are the different approaches of testing the element and the flux. There are some advanced use cases which we will be testing in the future tutorials. But this we came to the end of the story. Thank you for watching.