How to override TestMethod of MSTest - mstest

I have a situation where I have created a test framework on MSTEST.
Actually, we have integrated POM(Page object Model) using MSTEST
My TestSuite classes is using TestMethod annotation and those testsuites execute all testcase function define in another class.
I am generating trx report using below command line:-
MSTest.exe /testcontainer:"D:\AutomationScript\bin\Debug\AutomationScript.dll" /resultsfile:D:\Testcases_Reports\TestResultFinalTest.trx
In trx report all TestMethods which basically are my suites are coming while I need to append my testcases function name as well which are define in another class and called by my suites functions having TestMethod annotation
So How can I override TestMethod.
Any solution will be appreciated

Related

Run custom ApplicationContextInitializer in #SpringBootTest

i try to run a custum ApplicationContextInitializer within an integration test which is annotated with #SpringBootTest. I've tried to use a combination of #SpringBootTest and #ContextConfiguration, which looks like this:
#SpringBootTest
#ContextConfiguration(
initializers = CustomContextInitializer.class
)
public class Test {
....
}
This fails because some bean construction triggered by #SpringBootTest, depends on properties which will be injected by programmaticaly logic of my CustomContextInitializer and this one is executed parallel so that this properties aren't available at this point.
Is there a solution for this situation? Could the CustomContextInitializer run before the initalisation procedure triggered by #SpringBootTest?

spring application context in a set of unittest classes

I have created a couple unittest classes in the same package. All these classes have exactly one testcase and have the same annotation as shown below:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes= {TestConfig.class} )
When I run these testcases, I want each testcase be run in its own application context. But it seems all the testcases in the same package share one single application context, be it run from maven command line, or in Eclipse select the package to run as junit.
If I duplicate the TestConfig wiht names like TestConfig1, TestConfig2, etc. and annotate different test class with different TestConfig class, then each test will run in its own context instance.
Is there other elegant method to achieve this?
Thanks a lot.
This is the default behavior of Spring test. Spring test default caches the application context across the tests. This decreases the test execution time.
I dont know about your use case. If u have any where u are dirtying the application context(changing the state of the beans that are managed by springs that is affecting the subsequent tests ) then u can go for annotating the test method with #DirtiesContext. Spring test reloads the context for these methods. See below link how to use DirtiesContext.
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-ctx-management-caching
Use this feature carefully whenever needed only as this may shoot up your test execution time exponentialy.

Spring Cloud Task Integration Testing

I’m looking for some guidance on the best way of writing integration (ie tests for entire Spring Boot Application) for Spring Cloud Task.
Based on existing documentation and samples I see two approaches for this:
1) Use the standard #SpringBootTest with #TestPropertySource(properties = {"spring.cloud.task.closecontext_enable=false"}
as described here
http://docs.spring.io/spring-cloud-task/docs/1.2.0.M2/reference/htmlsingle/#_writing_your_test
This seems to allow effectively only one test per test class as the task is run when the spring context is initialized ie once per test class. However
#Autowiring of beans in the context into the test class should work to eg to check the results of the task, or examine the state of the task repository.
2) Use SpringApplication.run(MyTaskApplication.class, myArguments); in each test method as in the example here
https://github.com/spring-cloud/spring-cloud-task/blob/master/spring-cloud-task-samples/batch-job/src/test/java/io/spring/BatchJobApplicationTests.java
This allows me to write multiple tests in the test class each with potentially different spring properties or batch job parameters.
The main problem I have with either approach that I can’t see how to get access eg #Autowire to beans in the context such as JdbcTemplate (eg to insert test input data for a job into an embedded db) or RestTemplate (to set up expectations using MockRestServiceServer)
after these beans are created but BEFORE the task is run - is this possible? If not it’s hard to see how to write meaningful integration tests for tasks.
What Ive done for now is a variation on approach (2) above (ie I can run the task more than once / have multiple tests in the same test class)
I'm using
SpringApplication application = new SpringApplication(new Object[] {MyTaskApplication.class, TestConfig.class});
TestConfig is defined with #TestConfiguration in the test class and contains mock beans etc overriding the actual beans
I then use
application.addListeners()
to add a ContextRefreshedEventListener which allows me to set expectations on mocks (or execute jdbc calls) after the beans are created but before the task is run.
(I have a generic Listener class that allows me to pass in the behaviour as a lambda or method reference per bean)
Then run the task with
application.run(args);
(can use different args in different tests)
I can also pass "--spring.cloud.task.closecontext_enable=false" as an argument to keep the application open if i want to verify mocks / db state after the test runs. In this case I close it manually at the end of the test.
If this seems a sensible approach it might be useful if Spring Cloud Task itself provided some sort of generic listener or hook to allow the setting of test state between bean creation and task execution.

TestNG + Spring + Power mock Unit test

I have a Spring based application and am in the process of unit testing it. I'm using TestNG for unit tests. For my test i need to make use of PowerMockito to mock some static methods. I also need to make use of a test spring config file for only my unit test.
I'm unable to write my unit tests combining all the three i.e. TestNg, PowerMock and Spring.
I can combine TestNG and Spring by extending the class AbstractTestNGSpringContextTests, however cant mock static methods, instead it executes the actual static method. Something like the below:
#PrepareForTest(MyUtils.class)
#ContextConfiguration(locations = { "classpath:config/test-context.xml"})
public class MyImplTest extends AbstractTestNGSpringContextTests{
.....
}
I can combine TestNG with PowerMockito by extending the class PowerMockTestCase. But then the test spring config files are not resolved. Something like the below:
#PrepareForTest(MyUtils.class)
#ContextConfiguration(locations = { "classpath:config/test-context.xml"})
public class MyImplTest extends PowerMockTestCase{
.....
}
Is there any way for me to write my unit tests combining all the three, i.e. TestNg, PowerMockito and Spring context?
Rather than extending PowerMockTestCase, have you tried using the PowerMockObjectFactory by writing a method like below? Then you can extend AbstractTestNGSpringContextTests.
#ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
This is suggested by the Powermock GitHub docs.

Running testng with Spring context in Intellij Idea

When I run tests in my maven module from cmd I see that Spring context is available for all my tests even when they don't extend AbstractTestNGSpringContextTests and are not annotated with #ContextConfiguration.
However, when I run all tests in the test dir from Idea, some of the tests fail with NPE because #Autowired fields are not initialized. Most confusing is that, as I said, some tests pass and others don't even though they all don't extend AbstractTestNGSpringContextTests and are not annotated with #ContextConfiguration but all require Spring-injected fields in some of the classes. When I run tests separately in Idea, they always fail with NPE because there's no Spring injection. I'm new to testng and can't understand how the Suites are created and run with Spring context.
By the way, we tried it on Ubuntu machine, and the behaviour is not the same. Separate tests failed, but running the package succeeded without injection-related NPE issues.
Anyone encountered anything similar?
The problem seems to revolve around IntelliJ/testNG not processing 'groups' correctly. If a class "A" defines an autowired object, the #beforeGroups method in that class cannot use that object. But a different class "B" that extends class "A", and "B" has a #Test that uses the autowired object, then "B" can use the object (which is defined in "A", where it doesn't work!!!). Problem does not occur in Eclipse or with a Maven goal where surefire controls testNG.
The workaround for IntelliJ is to run an entire suite, instead of just a method or class. If we create a new suite file and strip out all the other classes, then we can run just one class.
See these issues in YouTrack
https://youtrack.jetbrains.com/issue/IDEA-135384
https://youtrack.jetbrains.com/issue/IDEA-125775
https://youtrack.jetbrains.com/issue/IDEA-110703
https://youtrack.jetbrains.com/issue/IDEA-111084

Resources