Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Created June 3, 2015 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcalmac/36a3271104fedb052aba to your computer and use it in GitHub Desktop.
Save bcalmac/36a3271104fedb052aba to your computer and use it in GitHub Desktop.
Experiment with recording expectations that throw exceptions
import java.sql.Connection;
import java.sql.SQLException;
import mockit.Expectations;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class JMockitExceptionTest {
@Test (expected = SQLException.class)
public void throwException(@Mocked Connection connection) throws SQLException {
new Expectations() {{
connection.close();
result = new SQLException("Simulated Exception");
}};
connection.close();
}
@Test
@Ignore
public void orderOfInvocation(@Mocked Connection connection) throws SQLException {
new NonStrictExpectations() {{
connection.getCatalog(); result = new SQLException();
connection.getCatalog(); result = "catalog";
}};
try {
connection.getCatalog();
fail();
} catch (SQLException e) {
assertEquals("catalog", connection.getCatalog());
}
}
@Test
public void orderOfInvocation2(@Mocked Connection connection) throws SQLException {
new NonStrictExpectations() {{
connection.getCatalog(); result = new Object[] {new SQLException(), "catalog"};
}};
try {
connection.getCatalog();
fail();
} catch (SQLException e) {
assertEquals("catalog", connection.getCatalog());
}
}
@Test
public void orderOfInvocation3(@Mocked Connection connection) throws SQLException {
new NonStrictExpectations() {{
connection.getCatalog(); result = new SQLException();
connection.getCatalog(); result = "catalog";
}};
assertEquals("catalog", connection.getCatalog());
assertEquals("catalog", connection.getCatalog());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment