下面是我在运行 Spring-Test-DBUnit 时遇到的错误。您在进行集成测试时是否遇到过同样的问题。您对如何解决此问题有任何想法吗?谢谢。
testCreatePerson(一些项目文件):无法加载 ApplicationContext testHomePage(一些项目文件):无法加载ApplicationContext testSameOriginXFrameOptions(某些项目文件):无法加载 ApplicationContext
我正在使用 spring mvc 创建一个 web 项目并想使用 spring test dbunit。
@Controller
public class PersonController
{
@RequestMapping( value = "/add-person", method = RequestMethod.POST )
public ResponseEntity<String> addPerson( @Validated @ModelAttribute PersonDB person, BindingResult bindingResult,
Principal principal ) throws Exception
{
handleValidationError( bindingResult );
String username = userAccountUtility.getUsernameFrom( principal );
personService.add( emil, username );
personConnectionManager.updatePersonConnectionMap( emil, username );
return new ResponseEntity<>( HttpStatus.OK );
}
}
/*******************************************/ BaseTestConfig
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import com.person.utils.OperatingSystem;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@ComponentScan( {"com.person", "com.person.commons"} )
@EnableWebMvc
@EnableTransactionManagement
@PropertySource( "classpath:application.properties" )
public class BaseTestConfig
{
public static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
public static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
public static final String PROPERTY_NAME_DATABASE_URL_LINUX = "db.url.linux";
public static final String PROPERTY_NAME_DATABASE_URL_WINDOWS = "db.url.windows";
public static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
@Resource
private Environment env;
@Autowired
private OperatingSystem operatingSystem;
@Bean
public DataSource dataSource()
{
String databaseUrl = PROPERTY_NAME_DATABASE_URL_LINUX;
if( operatingSystem.isWindows() )
{
databaseUrl = PROPERTY_NAME_DATABASE_URL_WINDOWS;
}
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName( env.getRequiredProperty( PROPERTY_NAME_DATABASE_DRIVER ) );
dataSource.setUrl( env.getRequiredProperty( databaseUrl ) );
dataSource.setUsername( env.getRequiredProperty( PROPERTY_NAME_DATABASE_USERNAME ) );
dataSource.setPassword( env.getRequiredProperty( PROPERTY_NAME_DATABASE_PASSWORD ) );
return dataSource;
}
@Bean
public UrlBasedViewResolver setupViewResolver()
{
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix( "/WEB-INF/pages/" );
resolver.setSuffix( ".jsp" );
resolver.setViewClass( JstlView.class );
return resolver;
}
}
/*******************************************/ SpringTest
//packages and imports
@WebAppConfiguration("src/test/resources")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BaseTestConfig.class)
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class
})
@DatabaseSetup("dataset/empty-person-dataset.xml")
public class EmilInstanceCreationTest {
@Resource
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webApplicationContextSetup(webApplicationContext)
.build();
}
private static final String firstname = "jane";
public static final String lastname = "cruz";
public static final String midllename = "garner";
@Autowired
private PersonController personController;
@Autowired
private PersonDB person;
@BeforeClass
public static void initContext() {
System.setProperty("spring.profiles.active", "dev");
System.setProperty("keycloak.configurationFile", "keycloak.json");
}
@Test
@ExpectedDatabase("dataset/add-person-dataset.xml")
public void testCreatePersonInstance() throws Exception {
BindingResult mockBindingResult = mock(BindingResult.class);
when(mockBindingResult.hasErrors()).thenReturn(false);
personController.addPerson(person, mockBindingResult, principals.create(firtstname, firstname, lastname));
}
@Bean(name = "dataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
}