Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does TestNG has runner like SpringJUnit4ClassRunner

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.

like image 320
Michał Mech Avatar asked Apr 12 '10 19:04

Michał Mech


People also ask

What is the difference between SpringJUnit4ClassRunner and SpringRunner?

There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner.

What is 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.

What does @RunWith SpringRunner class mean?

@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.


1 Answers

TestNG does not use Spring to instantiate your test. That's why someField=null

like image 110
abinet Avatar answered Sep 21 '22 17:09

abinet