TestNG #Factory analog in JUnit 5 - java

In TestNG we can annotate test class constructor #Factory annotation and specify data provider for this factory like this:
#Factory(dataProvider = "dataProviderName")
And TestNG create instance of test class for each object returned by data provider.
In JUnit 5 I didn't found feature exactly like that. My question is, how I can reproduce that behavior in JUnit 5?
Thanks in advance for your help

Please, read a guide about Dynamic tests - https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests-examples
Also, dynamic tests can be parameterized (like data providers) with Sources - https://junit.org/junit5/docs/current/user-guide/#writing-tests-dynamic-tests-uri-test-source read this part of guide

Related

How to pass multiple consumer names in pact provider JUnit test

Using the pact provider JUnit 5/Spring Boot support annotations, perhaps I am not searching well for the answer... I'm wondering if it's possible to annotate a pact provider verification test with multiple consumers using the #Consumer annotation.
Like I would want to be able to do something like the following
#Provider("provider-name")
#Consumer("consumer-1, consumer-2")
#PactBroker
#ActiveProfiles("test")
public class PactVerificationTest {
#Test
//test methods
//...
}
The annotation takes a String as a value so unfortunately something like #Consumer({"consumer-1", "consumer-2"}) does not work either.
Like this:
#PactBroker(consumerVersionSelectors = {
#VersionSelector(consumer = "my-consumer-1"),
#VersionSelector(consumer = "my-consumer-2")
})
Use the latest library version and see documentation for more

JUnit tests for Generic Links in tapestry

I have developed UI page which has href to external link. Now this external link is built (with db based key values for http request parameters) in a class which extends GenericLink of Tapestry. I am overriding public ILink getLink(IRequestCycle cycle) and public String getHref() methods for that. The developed task is working as expected.
Now I want to write JUnit tests for this class, but I read there is other mechanism for Unit testing in Tapestry but this is again not clear to me w.r.t. GenericLinks.
Any leads to any artical will really help in order to test GenericLink class.

Spring MVC - testing services

i have a question regarding service layer testing in Spring framework.
I have a class ProductService which has method:
public List<Product> getProducts() {
return productDAO.getProducts();
}
Is there any way to test it properly? This method retrieve all products using dao layer, when i, for example fill a list with five products(which are in database right now) and test this method by asserting size of the list it seems useless because when i add one product it still passes, but it shouldn't - after adding one product it should return 6 not 5 products...
If you have any usefull sites/yt videos about spring unit testing please let me know.
Thanks for Your help in advance!
Regards
One technique you could use to test this particular code is to mock the productDAO using a framework. The mock object then provides specific results that you can test for in your unit tests.

Alternatives to encode URL

I am trying to encode input form data here. There are two options and I have tried both of them:
Use URLencoder.encode(inputString) method which does not work on GWT client side (My code resides in client module) Results in error 'Did you forget to inherit required module?'
URL.encodeQueryString(inputString) which works well, But when I run relevant test cases using JUnit, all I get is unsatisfiedlinkederror
Are there any alternatives for encoding method or is there any work around for above mentioned methods?
For your second option :
GWT uses modules and needs to be compiled, which is different than running a simple JUnit test. Take a look at http://www.gwtproject.org/doc/latest/DevGuideTesting.html, they explain how to setup JUnit test.
Just use the URL class and its methods:
URL.encode(String decodedURL)
URL.encodeQueryString(String decodedURLComponent)
Do not forget to inherit the required module <inherits name="com.google.gwt.http.HTTP"/>.
For URL building, I use the "UrlBuilder": com.google.gwt.http.client.UrlBuilder
UrlBuilder u = new UrlBuilder();
u.setProtocol("https");
u.setHost("www.mysite.com:8080");
u.setPath("/myServletPath");
u.setParameter("username", nameField.getValue());
u.setParameter("someAttribute", "itsValue");
u.buildString();
This code will result in:
https://www.mysite.com:8080/myServlet?username=GWT%20User&someAttribute=itsValue

junit mock objects

i am new to junit so any help is appreciated.
I have a class called sysconfig.java for which i have written a
junit class file called TestSysconfig.java that tests some methods in the sysconfig.java.
The method that i am testing in sysconfig.java calls another class file "ethipmapping.java"
i have created a mock of this class file as Testethipmapping.java.
so my question is how do i tell sysconfig.java to call this mock object?
instead of mixing "new" operators with the class you're testing, you'll need to pass in your test instance of ethipmapping to the sysconfig class you are testing, either in the constructor, or via a setter. so, your class you are testing will look something like:
private EthipMapping mapping;
public Sysconfig(EthipMapping mapping) {
this.mapping = mapping;
}
public String someMethodIWantToTest() {
return mapping.doSomeStuffThatReturnsAString();
}
problems like this are why dependency injection frameworks like spring and google guice are so popular, although for simple cases like the above you don't need them.
You may be interested in Mockito.
so my question is how do i tell sysconfig.java to call this mock object?
By calling it. Wherever you created 'ethipmapping' object before (new ethipmapping()), you'll have to create Testethipmapping.
There are other options not involving changes in sysconfig.java, but it's difficult to give specific advice without seeing the code.

Categories