Handling Exceptions in JUnit Testing with JSON and Isomorphism Techniques

junit test example n.w
1 / 4
Embed
Share

Learn how to handle exceptions effectively in JUnit testing by exploring examples involving JSON manipulation and Isomorphism techniques. Enhance your understanding of error handling strategies in software development with detailed code snippets and explanations.

  • JUnit Testing
  • Exception Handling
  • JSON Manipulation
  • Isomorphism Techniques
  • Software Development

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Junit Test Example package stanford.cs190.tweeter; import org.junit.Test; public class JsonTest extends junit.framework.TestCase { @Test public void test_array() { Json document = new Json(); document.startArray(); document.addValue(12345); document.addValue(999); document.endArray(); assertEquals("[12345, 999]", document.toString()); } ... Do something } Check result CS 190 Lecture Notes: Exception Handling Slide 1

  2. Test Isomorphism protected void readRequest(Reader reader) { try { BufferedReader in = new BufferedReader(reader); // Read the first line of input // (method, url, protocol version). String firstLine = in.readLine(); if (firstLine == null) { throw new RequestError("empty HTTP request"); } String[] words = firstLine.split(" +", 4); if (words.length < 3) { throw new RequestError("syntax error in " + first line: '%s'", firstLine); } method = words[0]; rawUrl = words[1]; protocolVersion = words[2]; // Read the headers. Each iteration through the // following loop reads one line, which contains a // single header. while (true) { String headerLine = in.readLine(); if (headerLine == null) { throw new RequestError( "unexpected EOF while reading " + "headers"); } if (headerLine.length() == 0) { // Empty line marks the end of headers. break; } int i = headerLine.indexOf(": "); if (i == -1) { throw new RequestError("syntax error in " + "header line: '%s'", headerLine); } headers.put(headerLine.substring(0, i), headerLine.substring(i+2)); } ... @Test public void test_readRequest_basics() { ... } @Test public void test_readRequest_emptyInput() { ... } @Test public void test_readRequest_incompleteFirstLine() { ... } @Test public void test_readRequest_EofInHeaders() { ... } @Test public void test_readRequest_headerHasNoColon() { ... } CS 190 Lecture Notes: Exception Handling Slide 2

  3. Helper Methods /** * Create a bunch of new tweets in the current repo. * @param count * Number of tweets to create. */ protected void createTweets(int count) { ... } /** * Returns a string containing a sorted list of the * tweet ids for each of the TweetFiles in the * in-memory cache. */ protected String getCachedIds() { ... } CS 190 Lecture Notes: Exception Handling Slide 3

  4. Helper Methods, contd @Test public void test_checkCacheSize_belowLimit() { createTweets(17); repo.cacheLimit = 3; repo.checkCacheSize(); assertEquals("10 20", getCachedIds()); } CS 190 Lecture Notes: Exception Handling Slide 4

More Related Content