I would like to know, if it is possible to add parameters to Junit tests like you do it in testNg with the xml-Files. I know that you can have parameterized tests in Junit, but that's not what I want.
A short example how I do it now:
String example = "test"
String name = "test
...
@Test
public void testCase(){
Page.addParameter(example);
And I want to get rid of these variables in the method, and want to get it from a xml-File or an other method.
Does anybody have an idea how to solve that?
If you use Spring you can annotate JUnit test with @ContextConfiguration
passing some context.xml
configuration, where you can also specify String properties.
For this you have to add dependency on String Test (org.springframework:spring-test) and use it similar to this example (in Groovy):
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = ['/config/dao-context.xml'])
class ClientServiceImplTest {
...
}
UPDATE:
You can add following bean to your context.xml, to load properties from some .properties file
// context.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="location" value="file:test\resources\config\server.properties"/>
</bean>
<context:component-scan base-package="your.package" />
// Java code using property - autowired value by Spring
@Value("${server.dataPassword}")
private String dataPassword;
// server.properties
server.dataPassword=pwd
It seems like you can do it using this 3rd party project called junit-dataprovider on GitHub. You could basically return a Map<String,String>
object from the data-provider that has a single row of columns from a spreadsheet, passing it in as a single argument to your test method, and then you have access to all of those values as parameters. You could also pass in a "Page" object (or a Selenium helper object/driver) as an argument as well if you generated that within your data-provider.