About SpringJunit5 small problem test setup configuration file path

Today, encountered a small problem to solve and review recorded down in spring, the first thing we should be familiar with Junit4 test configuration is as follows:

@RunWith which is represented by Junit4, and @ContextConfiguration indicate the location of the configuration file , which @ContextConfiguration If no value, the default will be to find the class name from the current test and the test class at the same level in the current directory -context .xml configuration file . Such as: your test class name for App.class, then it will look for App-context.xml file in the same directory as shown in:

 

But then it becomes a test into Junit5

package cn.s.register;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import cn.s.register.action.UserAction;
import cn.s.register.dao.IUserDAO;
import cn.s.register.domain.User;

@SpringJUnitConfig
public class App {
@Autowired
UserAction action;
@Test
void test1() throws Exception {
	action.execute();
}
@Autowired
private IUserDAO dao;
@Test
void testName() throws Exception {
		User u = new User();
		u.setName("欧阳");
		u.setAge(14);
		dao.save(u);
}
}

Only a @SpringJUnitConfig and @Test, and then where to go to find the configuration file it?

In fact, we found the source code associated @SpringJUnitConfig which contains a @ContextConfiguration, this annotation we are not very familiar with? In JUnit4 is looking for the configuration file configuration. The Junit5 @ContextConfiguration that it is only a default is the current directory to find the name of the test -context profile

/**
 * {@code @SpringJUnitConfig} is a <em>composed annotation</em> that combines
 * {@link ExtendWith @ExtendWith(SpringExtension.class)} from JUnit Jupiter with
 * {@link ContextConfiguration @ContextConfiguration} from the <em>Spring TestContext
 * Framework</em>.
 *
 * @author Sam Brannen
 * @since 5.0
 * @see ExtendWith
 * @see SpringExtension
 * @see ContextConfiguration
 * @see org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig
 */
@ExtendWith(SpringExtension.class)
@ContextConfiguration
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SpringJUnitConfig {
----(因为篇幅问题- -)下面是SpringJUnitConfig的方法,大家可以自行去spring源码中查看
}

When we @ContextConfiguration that the property can be re-configured with a custom look for the path to the configuration file

package cn.s.register;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import cn.s.register.action.UserAction;
import cn.s.register.dao.IUserDAO;
import cn.s.register.domain.User;

@SpringJUnitConfig
@ContextConfiguration("classpath:cn/s/register/App-context.xml")
public class App {

@Autowired
private IUserDAO dao;
@Test
void testName() throws Exception {
		User u = new User();
		u.setName("欧阳");
		u.setAge(14);
		dao.save(u);
}
}

 

 

 

 

 

 

 

Released six original articles · won praise 3 · Views 793

Guess you like

Origin blog.csdn.net/SmileLucki/article/details/105264188
Recommended