spring with testNG without subclassing - java

is there any way to use testNG and spring without subclassing or copying half of AbstractTestNGSpringContextTests? is there anything simple like junit's #RunWith?

According to the Spring documentation, it doesn't look like it provides a TestNG test runner out of the box:
In addition to generic testing infrastructure, the TestContext framework provides explicit support for JUnit 3.8.2, JUnit 4.5+, and TestNG 5.10 in the form of abstract support classes. For JUnit 4.5+, the framework also provides a custom Runner that allows one to write test classes that are not required to extend a particular class hierarchy.
I suppose you could try to write a your own custom TestNG test runner that could do something similar.

Related

How to filter JUnit5 tests with custom annotation

I'm looking for a way to effectively target JUnit5 integration tests. Tests are run using gradle and all integration test classes are decorated by a custom class level annotation. I can filter them based on their class name pattern or by categorizing them using tag, but is there a way to setup a test suite that targets tests with annotation?
Assuming your custom class-level annotation is named #IntegrationTest, you have two options.
Meta-annotate #IntegrationTest with #Tag("integration-test") and then filter by the integration-test tag in your build.
Implement your own ExecutionCondition that enables/disables test classes based on the presence of #IntegrationTest and whatever other criteria you deem appropriate (for example, an environment variable or JVM system property). As an alternative to writing your own ExecutionCondition, you could meta-annotate #IntegrationTest with one of the built-in conditional execution annotations (such as #EnabledIfSystemProperty, #EnabledIfEnvironmentVariable, etc.)
You can find many alternatives
https://www.baeldung.com/junit-5-conditional-test-execution
for example you can use
#EnabledIfSystemProperty(named = "myProperty", matches = "my")
and then you can use program arguments or profiles for them in gradle

TestNG vs Junit vs Mockito?

I am having difficulty understanding the difference between these 3. Aren't all of these testing frameworks, and not libraries? If so, how is it that we are able to use these in combination? Shouldn't we be able to only use one testing framework, since the framework dictates the flow of control of code unlike in the case of libraries where we call the library code from our code. How can 2 independent frameworks work together on the same piece of code? I've observed that Mockito is used in conjunction with TestNG a lot. Isn't TestNG sufficient for most testing scenarios?
Both Testng and Junit are Testing framework used for Unit Testing. TestNG is similar to JUnit. Few more functionalities are added to it that makes TestNG more powerful than JUnit.
Annotations
Both JUnit and TestNG uses annotations and almost all the annotations looks similar.
TestNG uses #BeforeMethod ,#AfterMethod similar to #Before ,#After in JUnit4.
.........................
Mockito is a java based mocking framework, used in conjunction with other testing frameworks such as JUnit and TestNG.
It internally uses Java Reflection API and allows to create objects of a service. A mock object returns a dummy data and avoids external dependencies. It simplifies the development of tests by mocking external dependencies and apply the mocks into the code under test.
Summary
So as its name suggest Mockito is used for mocking the data or fake data which is act as real object to test, its acts like an stub or driver while still we need junit or testng with it. while Junit and TestNG is a unit testing framework of java
Explanation Tutorial
https://www.youtube.com/watch?v=eILy4p99ac8
Article on Mockito
https://www.journaldev.com/21816/mockito-tutorial

What's the difference between #RunWith(MockitoJUnitRunner.class) and #RunWith(SpringJUnit4ClassRunner.class)? When to use it appropriately?

What's the difference between #RunWith(MockitoJUnitRunner.class) and #RunWith(SpringJUnit4ClassRunner.class)? When to use it appropriately?
MockitoJUnitRunner
specific for use with the Mockito test framework
the Mockito framework helps with mocking dependencies when you want to focus your tests on a single class and avoid invoking methods on dependencies (instead invokes a mock/dummy that is easily configured).
Above is what mockito is used for, but for more on this runner specifically - from the docs: "keeps tests clean and improves debugging experience". "Runner is completely optional - there are other ways you can get #Mock working". Source - https://static.javadoc.io/org.mockito/mockito-core/2.6.8/org/mockito/junit/MockitoJUnitRunner.html
SpringJunit4ClassRunner
specific for use with the spring framework
used for integration tests when it is required to load the spring context (create spring beans, perform dependency injection, etc).
In integration tests you may not do as much mocking of dependencies but you can do both in the same test.
Integration tests are useful when you would like to test loading the spring context or perhaps test from the service/high level all the way down to lower levels like data access with a single test.
In some cases you may want to use both - like an integration test where you would also like to mock some dependencies (perhaps they make remote calls). Unfortunately you can't use two #RunWiths but this is a good post about that - Multiple RunWith Statements in jUnit

JUnit implementation of QAF

Good afternoon.
Is it possible to implement the work of QAF with JUnit?
For example, i want to use qaf-gherkin in my project, but it's build on JUnit + Cucumber.
As I see, there is a similar question, but there is no description about project and on which test framework it was based.
QAF built upon TestNG. If you are using Junit for Java you can run using TestNG by setting junit="true" in configuration file. However for migrating cucumber to QAF it should not impact current runner is either Junit or TestNG until you are using any specific features of Junit. All you need to start using QAF following documentation. You don't need to write or use additional java class to run your tests because it is taken care by QAF. All you need to do is create xml configuration file for to run your BDD
Because QAF provides all testing needs together you don't need to relay on multiple frameworks (junit and cucumber in your case).

AutoWiring steps with Spring Cucumber Serenity

I'm failing miserably to auto wire some steps.
To illustrate the point, I made a small sample project on github
https://github.com/lpicquet/serenity-cucumber-spring
I am trying to autowire steps so that I can share data between them but the test is currently failing. Anyone can help?
The issue is that you are using a different Runner. Generally people use the SpringRunner.class which handles the ability to create the test context etc.
Construct a new SpringRunner and initialize a TestContextManager to provide Spring testing functionality to standard JUnit 4 tests.
To use a different runner along with spring functionality you can use a combination of a ClassRule and a Rule
#ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
#Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
SpringClassRule is a custom JUnit TestRule that supports class-level features of the Spring TestContext Framework in standard JUnit tests by means of the TestContextManager and associated support classes and annotations.
In contrast to the SpringJUnit4ClassRunner, Spring's rule-based JUnit support has the advantage that it is independent of any Runner and can therefore be combined with existing alternative runners like JUnit's Parameterized or third-party runners such as the MockitoJUnitRunner.
In order to achieve the same functionality as the SpringJUnit4ClassRunner, however, a SpringClassRule must be combined with a SpringMethodRule, since SpringClassRule only supports the class-level features of the SpringJUnit4ClassRunner.
Without these there is no ability to Autowire within your dependant classes etc.
I've added a PR to your project with passing tests.

Categories