To get started, you need to download the latest version of EasyTest-Core module from teh Maven Central Repository.
<dependency> <groupId>org.easetech</groupId> <artifactId>easytest-core</artifactId> <version>1.1</version></dependency>
1) @TestConfigProvider - This annotation is used on the test class to identify where the configuration resides.
2) @TestBean - This is a method level annotation applied on the methods in the Config class(which is defined by @TestConfigProvider) to identify the test beans that should be injected into your test class.
3) @TestProperties - This is a class level annotation that provides convenient mechanism to load properties in your config class.Time for some examples.
2) @TestBean - This is a method level annotation applied on the methods in the Config class(which is defined by @TestConfigProvider) to identify the test beans that should be injected into your test class.
3) @TestProperties - This is a class level annotation that provides convenient mechanism to load properties in your config class.Time for some examples.
We will look at a very simple example for this blog post to introduce you to the idea of IoC in EasyTest and how to externalize your test configurations. You can then take it to any level of detail in your respective projects.
Lets assume I have a Service by the name ItemService that I want to test. Now this item service requires a lot of initialization before it can be used. Furthermore, lets assume that this service is used in more than one test. So each time I have to use it, I have to duplicate the code across tests. This makes my test filled with logic that ideally is not my test's concern. My test should worry about what method to test on the Service rather than how to initialize it.In the traditional world of testing, you would write a static method annotated with BeforeClass annotation and initialize it inside the test. In EasyTest world, the test is the main focus, not the configurations that goes around it. Let's see how we can leverage the above annotations to make our tests simpler to write and easier to maintain.
Step 1: We use the @TestConfigProvider annotation at the class level on the Test Class to tell the EasyTest Framework where to potentially get the Test Configurations from.
In the above example, @TestConfigProvider is used which takes an array of Class objects. In the above example the array consists of a single entry withe the name : TestConfigProviderClass Next lets look at the TestConfigProviderClass itself
@RunWith(DataDrivenTestRunner.class)
@TestConfigProvider({TestConfigProviderClass.class})
public class TestBeanConfigurations {
In the above example, @TestConfigProvider is used which takes an array of Class objects. In the above example the array consists of a single entry withe the name : TestConfigProviderClass Next lets look at the TestConfigProviderClass itself
1-> @TestProperties({"config.properties", "anotherConfig.properties"})
public class TestConfigProviderClass {
private Properties props;
2-> @TestBean("itemService") public RealItemService itemService(){
String propValue = props.getProperty("simple.property");
System.out.println(propValue);
return new RealItemService();
}
}
As you can notice, TestConfigProviderClass is a simple class that has two special annotations. Let's first talk about @TestBean annotation.
@RunWith(DataDrivenTestRunner.class)
@TestConfigProvider({TestConfigProviderClass.class})
public class TestBeanConfigurations {
->@Inject
->@Named("itemService")
public ItemService testSubject;
@Test
@DataLoader(filePaths={"overrideExcelData.csv"})
public Item getExcelTestDataWithDouble(@Param(name="libraryId") Double libraryId, @Param(name="itemId")Double itemId) {
Assert.assertNotNull(testSubject); Item item = testSubject.findItem(new LibraryId(Long.valueOf(libraryId.longValue())), new ItemId(Long.valueOf(itemId.longValue()))); return item;
}}
Thats it. Simple and fast. If you want to know more about the features provide by the EasyTest Framework, visit the GitHub site of EasyTest Core module and EasyTest Spring module.