Is there a way to include parameters in JUnit tests similar to how it is done in TestNG? If so, how can this

Is it possible to incorporate parameters into JUnit tests in a similar way as done in TestNG with xml files? I am aware of parameterized tests in JUnit, but that is not what I am looking for.

Currently, I handle it like this:

String example = "test";
String name = "test";
...
@Test
public void testCase(){
    Page.addParameter(example);

I would like to eliminate these variables within the method and instead retrieve them from an XML file or another method.

Any suggestions on how to achieve this?

Answer №1

When utilizing Spring, you have the option to annotate JUnit tests with @ContextConfiguration and provide a context.xml configuration file where String properties can be specified.

To achieve this functionality, you need to include a dependency on the Spring Test library (org.springframework:spring-test) and set it up similar to the following example (using Groovy):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = ['/config/dao-context.xml'])
class ClientServiceImplTest {
    ...
}

UPDATE:

You can add the following bean to your context.xml in order to load properties from a .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

Answer №2

If you're looking to streamline your testing process, consider utilizing the junit-dataprovider project available on GitHub. Instead of manually inputting data for each test case, you can return a Map object from the data-provider containing values from a spreadsheet in a single row. This map can be passed as an argument to your test method, granting you access to all necessary parameters. Additionally, you have the option to include a "Page" object or Selenium helper object/driver within your data-provider if needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extracting elements from a dynamic website within the <script> tag using the libraries BeautifulSoup and Selenium

I am currently working on scraping a dynamic website using beautifulsoup and selenium. The specific attributes I need to filter and export into a CSV are all located within a <script> tag. My goal is to extract the information found in this script: ...

Running JavaScript in the browser console using Python's Selenium

Looking to run a code through Firefox console with Selenium in Python. Does anyone have guidance on how to do this? ...

Using selenium to iterate through previous results and accumulate them in a for loop

My code is functioning, but it seems to be accumulating the previous results. Can someone please assist me with this issue? Thank you. url=https://www.bbc.com/news/world news_search = driver.find_elements(By.XPATH, "//div[@class='gs-c-promo gs-t ...

Iterating over a table and selecting a specific button on the rows that meet a specific condition

I'm having a bit of trouble and would really appreciate your help. I'm attempting to write a script that will scan through this table and delete any row that contains the word "active". The script needs to check every row in the table. The code ...

Tips for choosing the penultimate item in an unordered list using Selenium

Currently, I am involved in a selenium project where I need to scrape a website. The specific website contains an unordered list identified by the class name pagination. Here is how the code for the unordered list looks: <ul class="pagination" ...

How to extract information from a shared notebook on Evernote using Python

Trying to retrieve data from a shared Evernote notebook, such as this one: Initially attempted using Beautiful Soup: url = 'https://www.evernote.com/pub/missrspink/evernoteexamples#st=p&n=56b67555-158e-4d10-96e2-3b2c57ee372c' r = requests.g ...

Firefox seems to be disregarding the designated download location

My issue arises when attempting to download a file by clicking a button in Firefox. Everything seems to be working fine, except that the browser ignores the specified download directory and instead puts everything in the Downloads folder. While running t ...

Selenium - incapability to select the subsequent page

I'm experiencing difficulties with clicking the next button (>) and repeating this process until reaching the last page. Despite researching similar issues, I have been unable to identify what's causing my code to malfunction. Below is the co ...

Find the button for uploading files using Selenium

I am encountering an issue with trying to click the button displayed below (image and HTML). Despite attempting to locate it using both xpath and ID, Selenium is still unable to find it. https://i.stack.imgur.com/KMMku.png <input id="wsUpload1" type= ...

Contrast the schedules of two different calendars

Attempting to compare two timetables for a college project in order to identify available time slots in one timetable and concurrently check for events happening in the other. For instance, if a student has an open period from 4-5 on a Tuesday, the code sh ...

Unable to define the geckodriver system path through Python

I am facing a challenge trying to set the system path for geckodriver to use with Firefox on my OSX computer. Currently, I have it working smoothly for Chrome with the following setup: driver = webdriver.Chrome('/Users/Robert/Applications/chromedrive ...

The problem with importing a pre-defined Selenium function in the browser

Initial Set-up Working with selenium, I found myself constantly redefining functions. To streamline the process, I decided to create a separate file for these functions and import them as needed in my work files. A Basic Example Initially, when all fun ...

What is the best way to export a table element into a CSV file or data frame using Selenium/BeautifulSoup?

Here is the structure of my code: from bs4 import BeautifulSoup import requests from selenium import webdriver import time from selenium.webdriver.common.keys import Keys from selenium.webdriver import ActionChains from selenium.webdriver.common.keys impo ...

Tips for ensuring that each dropdown list is fully processed before proceeding to the next one

I am encountering an issue with navigating drop down menus using Selenium in Python. On this particular page, when I select an option from a drop down menu, it triggers some processing that temporarily makes the other options unselectable until the process ...

Utilizing Selenium for automating file downloads

Hey everyone, I'm trying to create a script using Selenium that will navigate through the pages on a website and save each page to a file. I need to scrape all 10 pages of reviews from this particular website. Here's the code I have so far: impo ...

NoseTest uncovers testing failures with Magic Mock

Currently, I am utilizing MagicMock for testing a function within a web application. This function is directly imported from a module. The expected functionality is as follows: when the tested function is called, it triggers a third-party API (although th ...

When trying to navigate through the PayPal login page, the Selenium webdriver abruptly stops without providing any error message

I'm facing an issue while trying to automate the payment process on paypal.com. After successfully loading the login page () and entering the email, the web driver unexpectedly closes when I click the next button without any error message appearing. ...

Unable to retrieve profile names from duplicate URLs with requests

Having an issue with logging into Facebook using selenium and transferring cookies to the requests module in order to collect profile names from specific URLs. The profile names on these URLs are static but require authentication. While my script successf ...

The entered password string in selenium python does not match

When using the send_keys method, sometimes a different password is entered resulting in login failure instead of the expected one. I have tried adding explicit waits and clearing the field but it did not solve the issue. Below is a Python code snippet dem ...

Instructions on obtaining the <h1> tags followed by the subsequent <h2> and <p> elements

Currently tackling a Selenium challenge using Python. I'm aiming to extract each element with an h1 tag, and once that's done, I want to fetch the nearest h2 and paragraph text tags to store the information in an object. This is what my code loo ...