Created
June 3, 2015 16:28
-
-
Save bcalmac/36a3271104fedb052aba to your computer and use it in GitHub Desktop.
Experiment with recording expectations that throw exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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