<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=yeecn" rel="nofollow">CN Yee</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-5404?redirect=false" rel="nofollow">SPR-5404</a></strong> and commented</p>
<p dir="auto">Spring beans are not available within <code class="notranslate">@BeforeTest</code> methods with TestNG tests, as follows:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="@ContextConfiguration(&quot;/applicationContext.xml&quot;)
public class Testing extends AbstractTestNGSpringContextTests {

	@Autowired
	SessionFactory sessionFactory;

	@Test
	public void testMe() {
		Session s = sessionFactory.openSession(); // This is OK
	}
	
	@BeforeTest  
	public void beforeTest() {
        	 Session s = sessionFactory.openSession(); // This give NullPointerException
	}
}"><pre class="notranslate"><code class="notranslate">@ContextConfiguration("/applicationContext.xml")
public class Testing extends AbstractTestNGSpringContextTests {

	@Autowired
	SessionFactory sessionFactory;

	@Test
	public void testMe() {
		Session s = sessionFactory.openSession(); // This is OK
	}
	
	@BeforeTest  
	public void beforeTest() {
        	 Session s = sessionFactory.openSession(); // This give NullPointerException
	}
}
</code></pre></div>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.6, 3.0.5</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398082391" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/8750" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/8750/hovercard" href="https://github.com/spring-projects/spring-framework/issues/8750">#8750</a> Should AbstractTestNGSpringContextTests use <code class="notranslate">@BeforeSuite</code> instead of <code class="notranslate">@BeforeClass</code>? (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto">5 votes, 4 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=sbrannen" rel="nofollow">Sam Brannen</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-4072?redirect=false" rel="nofollow">SPR-4072</a></strong> and commented</p>
<p dir="auto">AbstractTestNGSpringContextTests's springTestContextPrepareTestInstance() method is currently annotated with <code class="notranslate">@BeforeClass</code>, which prohibits <code class="notranslate">@BeforeSuite</code> methods in subclasses from interacting with the ApplicationContext.</p>
<p dir="auto">Essentially, the question raised in the forum is: would it be better to annotate the springTestContextPrepareTestInstance() method with <code class="notranslate">@BeforeSuite</code> instead of <code class="notranslate">@BeforeClass</code>?</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.6, 3.0.7, 3.1.1</p>
<p dir="auto"><strong>Reference URL:</strong> <a href="http://forum.springframework.org/showthread.php?p=148532#post148532" rel="nofollow">http://forum.springframework.org/showthread.php?p=148532#post148532</a></p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398092680" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10077" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10077/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10077">#10077</a> AbstractTestNGSpringContextTests: Spring beans not available within <code class="notranslate">@BeforeTest</code> methods (<em><strong>"is duplicated by"</strong></em>)</li>
</ul>
<p dir="auto">13 votes, 14 watchers</p>