Let’s store our test data (users) in test-users.json
in the resource
folder in JSON format.
Test data - resources/test-users.json
[
{
"firstName" : "John" ,
"lastName" : "Lennon" ,
"email" : "mail@john.lennon.com"
},
{
"firstName" : "Paul" ,
"lastName" : "McCartney" ,
"email" : "mail@paul.mccartney.com"
},
{
"firstName" : "Ringo" ,
"lastName" : "Starr" ,
"email" : "mail@ringo.starr.com"
},
{
"firstName" : "George" ,
"lastName" : "Harrison" ,
"email" : "mail@george.harrison.com"
}
]
The goal is to be able to inject that test data into any TestNG test as a regular Spring Boot bean.
See How to get started with TestNG and Spring Boot
Class User represents a model for test data in test-users.json
package io.lenar.examples.spring.model ;
public class User {
private String firstName ;
private String lastName ;
private String email ;
public User ( String firstName , String lastName , String email ) {
this . firstName = firstName ;
this . lastName = lastName ;
this . email = email ;
}
@Override
public boolean equals ( Object obj ) {
User user = ( User ) obj ;
return this . lastName . equals ( user . getLastName ())
&& this . firstName . equals ( user . getFirstName ())
&& this . email . equals ( user . getEmail ());
}
// setters and getters
application.properties
Here we store the name of the file that contains our test data.
See here how to get parameters from application.properties in Spring Boot
testData . users . file = test - users . json
FileReader class contains method for reading content of the file
See here how to read file into String
package io.lenar.examples.spring.testdata ;
import java.io.IOException ;
import java.io.InputStream ;
import java.nio.charset.StandardCharsets ;
import org.apache.commons.io.IOUtils ;
public class FileReader {
public String getFileAsString ( String fileName ) {
try {
InputStream input = this . getClass (). getResourceAsStream ( "/" + fileName );
return IOUtils . toString ( input , StandardCharsets . UTF_8 );
} catch ( IOException e ) {
// TODO Do all that you need to do if couldn't read a file
return null ;
}
}
}
Test data as a Spring bean
TestData
defines the Spring bean that can be injected where it’s needed. We need to inject our test data into TestNG tests. @Component
annotation informs Spring Boot that TestData should be considered as a Spring Boot bean and that allows us to inject TestData
into TestNG tests as we need or any other place in the project.
package io.lenar.examples.spring.testdata ;
import java.util.Arrays ;
import java.util.List ;
import io.lenar.examples.spring.model.User ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Component ;
import com.google.gson.Gson ;
@Component
public class TestData extends FileReader {
@Value ( "${testData.users.file}" )
private String usersFile ;
public List < User > getUsers () {
Gson gson = new Gson ();
return Arrays . asList ( gson . fromJson ( getFileAsString ( usersFile ), User []. class ));
}
}
Inject test data with @Autowired Spring annotation into TestNG tests
Now finally we can write tests that use our test data saved in the test-users.json
. All we need is to autowire TestData
with @Autowire
Spring annotation. Spring Boot will find, instantiate and inject TestData
into TestNG tests.
package io.lenar.examples.spring ;
import io.lenar.examples.spring.model.User ;
import io.lenar.examples.spring.testdata.TestData ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.boot.test.context.SpringBootTest ;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests ;
import org.testng.Assert ;
import org.testng.annotations.Test ;
@SpringBootTest ( classes = TestNGWithSpringApplication . class )
public class InjectTestDataIntoTestsIT extends AbstractTestNGSpringContextTests {
@Autowired
TestData testData ;
@Test
public void testUsersNumberTest () {
Assert . assertEquals ( testData . getUsers (). size (), 4 );
}
@Test
public void isRingoInBeatlesTest () {
User ringo = new User ( "Ringo" , "Starr" , "mail@ringo.starr.com" );
Assert . assertTrue ( isDudeInBeatles ( ringo ), "That dude (Ringo) is not in The Beatles" );
}
@Test
public void isElvisInBeatlesTest () {
User elvis = new User ( "Elvis" , "Presley" , "mail@elvis.presley.com" );
Assert . assertFalse ( isDudeInBeatles ( elvis ), "That dude (Elvis) is in The Beatles" );
}
private boolean isDudeInBeatles ( User dude ) {
return testData . getUsers (). stream (). anyMatch ( artist -> artist . equals ( dude ));
}
}
You may also find these posts interesting: