Traditional exception handling in JUnit tests
You’re probably used to testing code that throws exceptions in either one of the following two ways:
JUnit
's@Test
annotationexpected
parameter
You’re probably used to testing code that throws exceptions in either one of the following two ways:
JUnit
's @Test
annotation expected
parameterJUnit
is one of available Java libraries we can use to test our application code. Let’s get started by building
simple project using Maven
.
So, in case you haven’t read the first part in this series,
which is giving some basic introduction to the idea of JUnit
testing – go ahead, I’ll wait till you’re back so that we can
proceed with the next steps.
This is fairly short blog post. I recently had the case I needed to mock the same method twice during my Junit
test case, with different behavior.
If you're interested in JUnit
testing your Spring
MVC
controllers, feel free to visit Github
repo
containing very simple example of Spring
MVC
controller and JUnit
test class testing it's endpoints. Project is implemented
using Spring Boot
framework.
For all of you that are just new to the topic, I'll try to make a concise introduction to the idea of unit testing.
Unit testing allows us to test our functions (This is most often the case, but we can also unit test other things as well - e.g class instance state, etc) in isolation, so that we can verify implementation correctness.