On the one side, there is a new #RunWith annotation that lets to change unit test framework on the fly.
But on the other side Spring documentations says about org.springframework.test.annotation.ExpectedException:
#deprecated as of Spring 3.1 in favor of using built-in support for declaring expected exceptions in the underlying testing framework (e.g., JUnit, TestNG, etc.)
As a result my code will depend on the unit test framework. Please explain it.
And the 2nd question. At the moment I implement tests with Spring #RunWith annotation. But I also add the jUnit specific org.junit.Test annotation to each test method. Again, if I understand correctly the best way - to write tests, so I could change for example jUnit onto TestNg. And Spring #RunWith helps me to do that. But how can I avoid using of the org.junit.Test annotation?
#RunWith isn't a Spring annotation. It's a JUnit one. It doesn't let you switch between JUnit and TestNG, as you seem to think. Instead, it lets you run JUnit tests in different ways, like with the addition of the Spring Test Framework. In that framework, Spring has provided ExpectedException for some time, but it's no longer needed because recent versions of both JUnit and TestNG provide that functionality now.
You cannot write a test which can run on both JUnit and TestNG, so your code is bound to be dependent on testing framework. #RunWith is not a Spring's annotation for running tests with different testing frameworks, it belongs to JUnit and used to run JUnit with other runners like SpringJUnit4Runner to extend JUnit functionality
Related
I'm very novice to Unit Testing, I have created a Spring Boot Application and now I want to do some testing what's confusing me is where to use what e.g. I have classes and interfaces Controllers, Service, Repository I know each will have it's own Test class, so what will I be using let's say in Controllers, JUnit or Mockito ? similar question for Service and Repository.
conclusion: JUnit and Mockito can be used at same time, JUnit is used to do common test, and Mockito is used to mock objects.
In my view, JUnit is used more often, and Mockito is only used when I want to mock object, such as the object which is not finished yet. If you just want to test your Controller, Service and Repository, I recommend you to use JUnit. But if you need to mock some objects, you can use Mockito.
For example, when you test ServiceA, which depends on ServiceB, and ServiceB is not finished yet, you can use Mockito to mock ServiceB just to satisfy your requirements to test ServiceA
Hope this answer helps you! you can go to mockito and JUnit5 to learn more about them.
I'm trying to migrate my project to use Junit5. So far I've been using a class "LogSpy" that basically intercepts and saves all the logs so they can be tested easily. Using Junit4 and Spock test I was able to initialize my log interceptor class by using the #Rule annotation (even though it is in a Spock test). After migrating to Junit5 this annotation doesn't seem to initialize the needed log interceptor class and I can't find the reason why. Why did this happen? What are the differences between Junit4 and 5 regarding the #Rule annotation? Is there a way around this issue?
This is how I initialize the LogSpy class. It initializes in JUnit unit tests but not in Spock tests.
#Rule
public LogSpy logSpy = new LogSpy()
From the release notes
JUnit 4 Rules are not supported by spock-core anymore, however, there is a new spock-junit4 module that provides best effort support to ease migration.
In short add the spock-junit4 dependency, if you still need to use JUnit 4 rules.
In the long term, I would suggest to migrate to Spock native extensions.
I always add the #SpringBootTest to my test classes when using Spring Boot and my tests work as expected. I wonder what benefit do I get from also adding #RunWith(SpringRunner.class), since all over the internet that annotation is being used.
The #RunWith(SpringRunner.class) provides support for loading ApplicationContext and having beans #Autowired into your test instance.
I'm writing some tests with JUnit and Mockito.
I've noticed that Mockito provides a JUnit Runner and a JUnit TestRule
Which are the pros and cons of each solution ?
In general, a rule provides more flexibility than a runner. There can be only one runner, whereas you can have multiple rules in one test class.
Since Mockitos runner and rule apparently do the same I don't see a reason to use the runner here.
For the sake of completeness, I'd like to mention that there is no need to use Mockito's rules (or runners) unless you want to use mock annotations or validateMockitoUsage().
For consistency I usually create all mocks with mock() as quite often tests have mocked fields as well as mocked local variables.
A test suite has been developed using WebDriver and JUnit4. Its working fine, but now we need to integrate test suite with Jmeter for load testing. The problem is that the classes use the Annotation of "#RunsWith" to test a test case with multiple inputs and JMeter does not support this annotation.
Is there any workaround available ? (I could not find any, but may be some one has tried something)
Is this really a good approach, to first write test cases using web driver
Maybe, you just has old version of JMeter that works only with JUnit 3.x. In such a case you have only 2 choses: rewerite your JUnit in 3.x style or upgrade JMeter.