Project

General

Profile

Revision c3233f9b

IDc3233f9bb44cddd7f3d3e67ebfb976c9ee0dd4af
Parent 7d60d703
Child b064a5d4

Added by Andreas Kohlbecker over 3 years ago

moving AlternativeUnitilsJUnit4TestClassRunner to org.unitils

View differences:

cdmlib-test/src/main/java/org/unitils/AlternativeUnitilsJUnit4TestClassRunner.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package org.unitils;
10

  
11
import java.io.IOException;
12
import java.io.InputStream;
13

  
14
import org.junit.internal.runners.InitializationError;
15
import org.junit.runner.notification.RunNotifier;
16
import org.unitils.UnitilsJUnit4TestClassRunner;
17

  
18
/**
19
 * The CDM integration tests can not yet use the {@link org.springframework.test.context.junit4.SpringJUnit4ClassRunner SpringJUnit4ClassRunner}
20
 * provided by the spring test framework. Due to this it is not possible to use
21
 * {@code @TestPropertySource(properties = {"myproperty = foo"})} to inject properties into the spring environment which are
22
 * needed to configure the tests. This <code>ClassRunner</code> is simple alternative which also allows to inject
23
 * properties into the spring environments. In contrast to the {@link org.springframework.test.context.junit4.SpringJUnit4ClassRunner SpringJUnit4ClassRunner}
24
 * it loads the properties from a test resource file named <code>spring-environment.mock.properties</code>
25
 *
26
 *
27
 * @author a.kohlbecker
28
 * @since Nov 23, 2017
29
 *
30
 */
31
public class AlternativeUnitilsJUnit4TestClassRunner extends UnitilsJUnit4TestClassRunner {
32

  
33
    /**
34
     * @param testClass
35
     * @throws InitializationError
36
     */
37
    public AlternativeUnitilsJUnit4TestClassRunner(Class<?> testClass) throws InitializationError {
38
        super(testClass);
39
    }
40

  
41
    /**
42
     * {@inheritDoc}
43
     */
44
    @Override
45
    public void run(RunNotifier notifier) {
46

  
47
        loadSystemPropertiesFrom("spring-environment.mock.properties");
48
        super.run(notifier);
49
    }
50

  
51
    /**
52
     * @param propFile
53
     */
54
    protected void loadSystemPropertiesFrom(String propFile) {
55
        InputStream inStream = this.getClass().getClassLoader().getResourceAsStream(propFile);
56
        //Properties props = new Properties();
57
        try {
58
            // props.load(inStream);
59
            System.getProperties().load(inStream);
60
        } catch (IOException e) {
61
            throw new RuntimeException(e);
62
        }
63
    }
64

  
65

  
66

  
67
}
cdmlib-test/src/main/java/unitils/AlternativeUnitilsJUnit4TestClassRunner.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package unitils;
10

  
11
import java.io.IOException;
12
import java.io.InputStream;
13

  
14
import org.junit.internal.runners.InitializationError;
15
import org.junit.runner.notification.RunNotifier;
16
import org.unitils.UnitilsJUnit4TestClassRunner;
17

  
18
/**
19
 * The CDM integration tests can not yet use the {@link org.springframework.test.context.junit4.SpringJUnit4ClassRunner SpringJUnit4ClassRunner}
20
 * provided by the spring test framework. Due to this it is not possible to use
21
 * {@code @TestPropertySource(properties = {"myproperty = foo"})} to inject properties into the spring environment which are
22
 * needed to configure the tests. This <code>ClassRunner</code> is simple alternative which also allows to inject
23
 * properties into the spring environments. In contrast to the {@link org.springframework.test.context.junit4.SpringJUnit4ClassRunner SpringJUnit4ClassRunner}
24
 * it loads the properties from a test resource file named <code>spring-environment.mock.properties</code>
25
 *
26
 *
27
 * @author a.kohlbecker
28
 * @since Nov 23, 2017
29
 *
30
 */
31
public class AlternativeUnitilsJUnit4TestClassRunner extends UnitilsJUnit4TestClassRunner {
32

  
33
    /**
34
     * @param testClass
35
     * @throws InitializationError
36
     */
37
    public AlternativeUnitilsJUnit4TestClassRunner(Class<?> testClass) throws InitializationError {
38
        super(testClass);
39
    }
40

  
41
    /**
42
     * {@inheritDoc}
43
     */
44
    @Override
45
    public void run(RunNotifier notifier) {
46

  
47
        loadSystemPropertiesFrom("spring-environment.mock.properties");
48
        super.run(notifier);
49
    }
50

  
51
    /**
52
     * @param propFile
53
     */
54
    protected void loadSystemPropertiesFrom(String propFile) {
55
        InputStream inStream = this.getClass().getClassLoader().getResourceAsStream(propFile);
56
        //Properties props = new Properties();
57
        try {
58
            // props.load(inStream);
59
            System.getProperties().load(inStream);
60
        } catch (IOException e) {
61
            throw new RuntimeException(e);
62
        }
63
    }
64

  
65

  
66

  
67
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)