Merge pull request #10074 from eugenp/bfontana/BAEL-4337
BAEL-4337 - Conditionally ignoring or running tests in JUnit 4
This commit is contained in:
		
						commit
						71623e7880
					
				| @ -5,37 +5,59 @@ import static org.junit.Assert.assertEquals; | |||||||
| import static org.junit.Assume.assumeFalse; | import static org.junit.Assume.assumeFalse; | ||||||
| import static org.junit.Assume.assumeThat; | import static org.junit.Assume.assumeThat; | ||||||
| import static org.junit.Assume.assumeTrue; | import static org.junit.Assume.assumeTrue; | ||||||
|  | import static org.junit.Assume.assumeNotNull; | ||||||
|  | import static org.junit.Assume.assumeNoException; | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| public class ConditionallyIgnoreTestsUnitTest { | public class ConditionallyIgnoreTestsUnitTest { | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     @Test |     @Test | ||||||
|     public void whenAssumeThatCodeVersionIsNot2_thenIgnore() { |     public void whenAssumeThatAndOSIsLinux_thenRunTest() { | ||||||
|         final int codeVersion = 1; |         assumeThat(getOsName(), is("Linux")); | ||||||
|         assumeThat(codeVersion, is(2)); |         assertEquals("run", "RUN".toLowerCase()); | ||||||
| 
 |  | ||||||
|         assertEquals("hello", "HELLO".toLowerCase()); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenAssumeTrueOnCondition_thenIgnore() { |     public void whenAssumeTrueAndOSIsLinux_thenRunTest() { | ||||||
|         final int codeVersion = 1; |         assumeTrue(isExpectedOS(getOsName())); | ||||||
|         assumeTrue(isCodeVersion2(codeVersion)); |         assertEquals("run", "RUN".toLowerCase()); | ||||||
| 
 |  | ||||||
|         assertEquals("hello", "HELLO".toLowerCase()); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void whenAssumeFalseOnCondition_thenIgnore() { |     public void whenAssumeFalseAndOSIsLinux_thenIgnore() { | ||||||
|         final int codeVersion = 2; |         assumeFalse(isExpectedOS(getOsName())); | ||||||
|         assumeFalse(isCodeVersion2(codeVersion)); |         assertEquals("run", "RUN".toLowerCase()); | ||||||
| 
 |  | ||||||
|         assertEquals("hello", "HELLO".toLowerCase()); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private boolean isCodeVersion2(final int codeVersion) { |     @Test | ||||||
|         return codeVersion == 2; |     public void whenAssumeNotNullAndNotNullOSVersion_thenRun() { | ||||||
|  |         assumeNotNull(getOsName()); | ||||||
|  |         assertEquals("run", "RUN".toLowerCase()); | ||||||
|     } |     } | ||||||
| } | 
 | ||||||
|  |     /** | ||||||
|  |      * Let's use a different example here. | ||||||
|  |      */ | ||||||
|  |     @Test | ||||||
|  |     public void whenAssumeNoExceptionAndExceptionThrown_thenIgnore() { | ||||||
|  |         assertEquals("everything ok", "EVERYTHING OK".toLowerCase()); | ||||||
|  |         String t = null; | ||||||
|  |         try { | ||||||
|  |           t.charAt(0); | ||||||
|  |         } catch (NullPointerException npe) { | ||||||
|  |           assumeNoException(npe); | ||||||
|  |         } | ||||||
|  |         assertEquals("run", "RUN".toLowerCase()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private boolean isExpectedOS(final String osName) { | ||||||
|  |         return "Linux".equals(osName); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // This should use System.getProperty("os.name") in a real test. | ||||||
|  |     private String getOsName() { | ||||||
|  |         return "Linux"; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user