java


Parameterised Test Execution in JUnit Jupiter(JUnit5)


Is there a way to have parameterized tests in JUnit Jupiter (Junit5)?
#RunWith(Parameterized.class)
Background:
I went through How to write junit tests for interfaces? and wanted to implement test cases as answered here using JUnit5. But couldn't find the same class/es in the Jupiter package.
Attempts:
Looking further for substitutes I found that Extensions would be replacing the #RunWith as stated in Migrating tips
#RunWith no longer exists; superseded by #ExtendWith.
I tried defining a CustomExtension as provided in the sample here - MockitoExtension but couldn't succeed in getting to use the instances of classes Parameterized.
Looking forward to suggestions on how to test interfaces with parameterised instances of classes implementing it in JUnit5.
JUnit 5 M4 was just released and now supports parameterized tests.
Here's the "Hello World" example:
#ParameterizedTest
#ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
assertNotNull(argument);
}
Please refer to the User Guide for the full documentation and additional examples.
As of January 9th, 2017, JUnit 5 does not support parameterized tests per se, but work on that feature is in progress. Note that you might be able to achieve something similar with dynamic tests.
That said, I would consider both bad ways to implement interface tests and JUnit Jupiter offers two better approaches:
#Test annotated interface default methods will be executed. So if you have a production interface Interface you can write an interface InterfaceTest that uses default methods to test everything you want. For every Impl implements Interface you can then write an ImplTest implements InterfaceTest.
If you need more flexibility and would like to implement the tests for Interface in an abstract class AbstractInterfaceTest you can do that as well and then have a nested test class in your ImplTest that extends AbstractInterfaceTest.
Both of these approaches ensure that the test for the interface does not know the classes implementing the interface, which is a huge downside of the answer you linked to.
In my projects i have been using this library:
https://github.com/TNG/junit-dataprovider/
in order to add parameterization to my JUnit tests.
I have been using version 4.x but was never a fan of the embedded parameterization it provided.
If you are familiar with TestNg's #DataProvider then this extension is not much different.
Check it out as see if it will be of any benefit to you as it was for me and my team. Frankly i could not imagine working without it now.
You can use dynamic tests for that:
#TestFactory
Stream<DynamicTest> params() {
return Stream.of(new double[][]{{2d, 4d}, {3d, 9d}, {4d, 16d}}).
map(i ->
dynamicTest("Square root test " + Arrays.toString(i),
() -> assertEquals(i[0], Math.sqrt(i[1]))
)
);
}

Related Links

java, class not found exception com.mysql.jdbc.Driver [duplicate]
Method: “getMap” and “map” doesn't work
Spring load properties from database using Factory Bean of Properties
Feign - handling underling Exceptions - propagating error status
Rich user interface designing and development for android
G1 GC — extremely long termination time
How to use ACTIONS class in Jmeter-webdriver.plugin. - Webdriver Sampler.?
Error:CreateProcess error=216 - Android Studio 2.2
Vim: Indent code for parenthesis just like it does for braces
How much time the user focus on specific element on RecycleView in android
graphics.setColor(color); not working
How to init and fill Map in java 8
Unable to start an activity on button click
Software design solution - Game [closed]
Spring boot app memory increasing linearly and crashes
confused by synchronized usage

Categories

HOME
osgi
jdo
hook
mockito
iot
q
fingerprint
acquia
indesign
contact
facebook-messenger-bot
slurm
spring-kafka
google-apps-marketplace
zend-framework3
percona
uitypeeditor
dosgi
finite-automata
facebook-page
amazonsellercentral
zapier
clickonce
tokenize
cloudhub
chromium-embedded
buildbot
cas
user-interaction
minitab
neo4j-spatial
buck
react-chartjs
http-get
lxd
android-nestedscrollview
ios5
revolution-slider
stacked
space-complexity
hockeyapp
xenforo
webix-treetable
awt
email-templates
google-closure
firebase-admin
midl
cubic-spline
rails-routing
fancybox-2
gabor-filter
tropo
redundancy
migradoc
abcpdf9
nxt
savon
gridview-sorting
lttng
disque
moveit
ubuntu-10.04
nessus
ios4
historian
ruby-2.2
drawbitmap
0xdbe
appfabric-cache
apache-commons-fileupload
p2
dd
coldbox
document-classification
hyprlinkr
didselectrowatindexpath
ember-charts
srs
mysqltuner
ivyde
dataadapter
django-nonrel
xamlparseexception
limejs
selected
anonymous-methods
path-separator
first-responder
chrono
netdna-api
subgurim-maps
coercion
cinema-4d
appender
celltable
asp.net-mvc-areas
mozilla-prism
xsdobjectgen
meego
kdbg

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App