0
以下是我在运行Spring-Test-DBUnit时遇到的错误。进行集成测试时您遇到过同样的问题吗?你有什么想法解决这个问题吗?谢谢。弹簧测试DB单元:未能加载应用程序上下文
testCreatePerson(some project file): Failed to load ApplicationContext testHomePage(some project file): Failed to load ApplicationContext testSameOriginXFrameOptions(some project file): Failed to load ApplicationContext
我使用Spring MVC中创建一个Web项目,并希望使用Spring测试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;
}
}
你能提供更多的信息吗?像测试你试图运行... – juanlumn
请在上面的其他信息。 – James