When I write tests in JUnit (in Spring context) I usualy do it like this:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:testContext.xml") public class SimpleTest { @Test public void testMethod() { // execute test logic... } }
How can I do the same with TestNG?
I'll add more details. With AbstractTestNGSpringContextTests it works, but not in a way I want to. I have some test ...
@ContextConfiguration(locations = { "classpath:applicationContextForTests.xml" }) public class ExampleTest extends AbstractTestNGSpringContextTests { private Boolean someField; @Autowired private Boolean someBoolean; @Test public void testMethod() { System.out.println(someField); Assert.assertTrue(someField); } @Test public void testMethodWithInjected() { System.out.println(someBoolean); Assert.assertTrue(someBoolean); } // setters&getters }
and descriptor ...
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="exampleTest" class="pl.michalmech.ExampleTest"> <property name="someField"> <ref bean="someBoolean"/> </property> </bean> <bean id="someBoolean" class="java.lang.Boolean"> <constructor-arg type="java.lang.String" value="true"/> </bean> </beans>
The results are ...
null true Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.599 sec <<< FAILURE! Results : Failed tests: testMethod(pl.michalmech.ExampleTest)
That's why I asked about runner.
There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner.
SpringJUnit4ClassRunner is a custom extension of JUnit's BlockJUnit4ClassRunner which provides functionality of the Spring TestContext Framework to standard JUnit tests by means of the TestContextManager and associated support classes and annotations.
@RunWith(SpringRunner. class) tells JUnit to run using Spring's testing support. SpringRunner is the new name for SpringJUnit4ClassRunner , it's just a bit easier on the eye. @SpringBootTest is saying “bootstrap with Spring Boot's support” (e.g. load application.
TestNG does not use Spring to instantiate your test. That's why someField=null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With