Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test.loginController;
- import static org.hamcrest.Matchers.hasProperty;
- import static org.hamcrest.Matchers.isEmptyOrNullString;
- import static org.hamcrest.Matchers.nullValue;
- import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get;
- import static org.springframework.test.web.server.result.MockMvcResultMatchers.forwardedUrl;
- import static org.springframework.test.web.server.result.MockMvcResultMatchers.model;
- import static org.springframework.test.web.server.result.MockMvcResultMatchers.status;
- import static org.springframework.test.web.server.result.MockMvcResultMatchers.view;
- import java.io.InputStreamReader;
- import java.sql.Connection;
- import javax.sql.DataSource;
- import org.dbunit.JdbcDatabaseTester;
- import org.dbunit.dataset.IDataSet;
- import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
- import org.h2.jdbcx.JdbcDataSource;
- import org.h2.tools.RunScript;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.TestExecutionListeners;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
- import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
- import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
- import org.springframework.test.web.server.MockMvc;
- import com.github.springtestdbunit.DbUnitTestExecutionListener;
- import com.github.springtestdbunit.annotation.DatabaseSetup;
- import com.github.springtestdbunit.annotation.ExpectedDatabase;
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration
- @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
- TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
- @DatabaseSetup("login.xml")
- public class LoginControllerTest2 {
- private static final String JDBC_DRIVER = "org.h2.Driver";
- private static final String JDBC_URL = "jdbc:h2:mem:login;DB_CLOSE_DELAY=-1";
- private static final String USER = "sa";
- private static final String PASS = "";
- private static final String SCHEMA_FILE = "../sql/h2.sql";
- private static final String DATASET = "login.xml";
- private MockMvc mockMvc;
- @BeforeClass
- public static void createSchema() throws ClassNotFoundException {
- Class.forName(JDBC_DRIVER);
- try {
- Connection conn = dataSource().getConnection();
- InputStreamReader in = new InputStreamReader(LoginControllerTest2.class.getResourceAsStream(SCHEMA_FILE));
- RunScript.execute(conn, in);
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- @Before
- public void loadTestData() throws Exception {
- IDataSet ids = new FlatXmlDataSetBuilder().build(LoginControllerTest2.class.getResourceAsStream(DATASET));
- JdbcDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASS);
- databaseTester.setSetUpOperation(org.dbunit.operation.DatabaseOperation.CLEAN_INSERT);
- databaseTester.setDataSet(ids);
- databaseTester.onSetup();
- }
- private static DataSource dataSource() {
- JdbcDataSource dataSource = new JdbcDataSource();
- dataSource.setURL(JDBC_URL);
- dataSource.setUser(USER);
- dataSource.setPassword(PASS);
- return dataSource;
- }
- @Test
- @ExpectedDatabase("login.xml")
- public void testShowForm() throws Exception {
- mockMvc.perform(get("/login")).andExpect(status().isOk()).andExpect(view().name("/login"))
- .andExpect(forwardedUrl("../../WebContent/j/login.jsp"))
- .andExpect(model().attribute("form", hasProperty("passportId", nullValue())))
- .andExpect(model().attribute("form", hasProperty("email", isEmptyOrNullString())))
- .andExpect(model().attribute("form", hasProperty("username", isEmptyOrNullString())))
- .andExpect(model().attribute("form", hasProperty("hostname", isEmptyOrNullString())))
- .andExpect(model().attribute("form", hasProperty("pass", isEmptyOrNullString())));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement