Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Commit

Permalink
Add "test" configuration profile for tests
Browse files Browse the repository at this point in the history
Looks like test.application.properties was needed after all. :-) I
replaced it with proper use of configuration profiles:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-profiles
  • Loading branch information
denisw committed Jan 23, 2015
1 parent 07bae34 commit b208eac
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 61 deletions.
14 changes: 14 additions & 0 deletions config/application-test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This configuration file is loaded additionally to applications.properties
# during unit and integration tests. The options defined in this file override
# the ones in application.properties.

# Set this to true to test with the PDC result database turned on.
# Note that this requires a locally running MongoDB instance.
ddb.storage.enable=false

# Test database settings. If you have enabled the database, make sure that
# these are different from your development and production settings.
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=pdc
spring.data.mongodb.collection=pdc_results_test
2 changes: 2 additions & 0 deletions src/test/java/de/ddb/pdc/PdcIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.client.MockRestServiceServer;
Expand All @@ -29,6 +30,7 @@
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Main.class)
@WebAppConfiguration
@ActiveProfiles({"test"})
@IntegrationTest("server.port:0")
public class PdcIntegrationTest {

Expand Down
9 changes: 5 additions & 4 deletions src/test/java/de/ddb/pdc/storage/MongoStorageServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.ddb.pdc.storage;

import de.ddb.pdc.Main;
import de.ddb.pdc.core.Answer;
import de.ddb.pdc.core.AnsweredQuestion;
import de.ddb.pdc.core.PDCResult;
Expand All @@ -11,13 +12,13 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestConfiguration.class,
loader = AnnotationConfigContextLoader.class)
@SpringApplicationConfiguration(classes = Main.class)
@ActiveProfiles({"test"})
public class MongoStorageServiceTest {

@Autowired
Expand Down
53 changes: 0 additions & 53 deletions src/test/java/de/ddb/pdc/storage/TestConfiguration.java

This file was deleted.

9 changes: 5 additions & 4 deletions src/test/java/de/ddb/pdc/web/PDCControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package de.ddb.pdc.web;

import de.ddb.pdc.Main;
import de.ddb.pdc.core.Answer;
import de.ddb.pdc.core.PublicDomainCalculator;
import de.ddb.pdc.core.AnsweredQuestion;
Expand All @@ -11,21 +12,21 @@
import de.ddb.pdc.metadata.DDBItem;
import de.ddb.pdc.metadata.MetaFetcher;
import de.ddb.pdc.storage.StorageService;
import de.ddb.pdc.storage.TestConfiguration;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=TestConfiguration.class,
loader = AnnotationConfigContextLoader.class)
@SpringApplicationConfiguration(classes = Main.class)
@ActiveProfiles({"test"})
public class PDCControllerTest {

@Autowired
Expand Down

4 comments on commit b208eac

@phil-fuber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solution regarding the test configuration. So the profile is set via the command line argument and this code only runs in the test profile. And since the profile is called test, it will automatically use the properties under application-test.properties. Quite neat.

@denisw
Copy link
Contributor Author

@denisw denisw commented on b208eac Jan 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The profile is actually not chosen as a command-line parameter in this case. Instead, the test classes which Use Spring4JUnitRunner have a @ActiveProfiles({"test"}) annotation attached to them.

@phil-fuber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see so you are switching the profile to test explicity here for those test classes?

@denisw
Copy link
Contributor Author

@denisw denisw commented on b208eac Jan 23, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct. This makes it impossible to run the tests "the wrong way" (by not specifying a profile explicitly).

Please sign in to comment.