Junit Deepdive Part1quiz
This short review quiz is related to the previous article here.
1. What JUnit version introduced annotations for handling the simple lifecycle?
- since 1.0
- since 3.6
- since 4.0
- since 4.7
3. Why should you prefer @Before annotated methods in comparison to the constructor? Because
- it does not break the symmetry of the lifecycle
- @Before should not be preferred
- @Before methods handle exceptions while constructors not
- other frameworks rely on that
- yes
- no
5. Which method qualifiers must be used for a @AfterClass method?
- protected
- static
- public
- final
6. What happens if a public method is annotated with @Before, @Test and @After?
- Runtime-/ValidationError
- CompilerError
- Runs fine
7. What is the simplest way (considering clean code) to handle exceptions?
- use try/catch
- use @Test(expected=....class)
- it is integrated in JUnit
1 public class LifecycleTest {
2
3 @Before
4 public void before() {
5 System.out.print("3");
6 }
7
8 public LifecycleTest() {
9 System.out.print("2");
10 }
11
12 @BeforeClass
13 public static void beforeClass() {
14 System.out.print("1");
15 }
16
17 @Test
18 public void test() {
19 System.out.print("4");
20 }
21
22 @Test
23 public void test2(){
24 System.out.print("5");
25 }
26
27 @After
28 public void after() {
29 System.out.print("6");
30 }
31 }
- RuntimeException
- 123456
- 123462356
- Not predictable, depends on JVM
The solutions can be found here in white color: 1c; 2acd; 3a; 4bc; 5c; 6b; 7d;