Javaプログラミングで避けるべき9つの間違い
Javaでのプログラミングで避けるべき間違いとは?次の記事では、この質問にお答えします。
コンテナ・フォーム・テストの実行方法については、Java関連の記事でシニアJava開発者がすべてのマジックを紹介しています。
私はこうしている。 スプリングブート での プロジェクト を使うことで、定型文を減らすことができる。テスト・コンテナは スプリングフレームワーク そして、それを使わずに使うこともできる。
Testcontainersのバージョンは1.17.3を使用していますが、最新のものをご自由にお使いください。Postgresコンテナでのテスト。
まずコンテナを定義する:
public class Postgres13TC extends PostgreSQLContainer { 次のようになります。
private static final Postgres13TC TC = new Postgres13TC();
プライベートPostgres13TC()
super("postgres:13.2");
}
public static Postgres13TC getInstance() { { { { { Postgres13TCを取得します。
TC を返します;
}
オーバーライド
public void start() {
super.start();
System.setProperty("DB_URL", TC.getJdbcUrl());
System.setProperty("DB_USERNAME", TC.getUsername());;
System.setProperty("DB_PASSWORD", TC.getPassword());
}
オーバーライド
public void stop() { // 何もしない。
// 何もしない。これは共有インスタンスだ。この操作はJVMに任せてください。
}
}
その後、Springアプリケーションコンテキストを初期化します。コンテナインスタンスからすべてのデータソース設定を取得し、Springの設定として設定する:
public class ContainerInit implements ApplicationContextInitializer {.
public static Postgres13TC postgres13TC;
スタティック
postgres13TC = Postgres13TC.getInstance();
postgres13TC.start();
}
オーバーライド
public void initialize(ConfigurableApplicationContext applicationContext) { } @override
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(
applicationContext、
"spring.datasource.url=" + postgres13TC.getJdbcUrl()、
"spring.datasource.username=" + postgres13TC.getUsername()、
"spring.datasource.password=" + postgres13TC.getPassword()、
"db.host=" + postgres13TC.getHost()、
"db.port=" + postgres13TC.getMappedPort(postgres13TC.POSTGRESQL_PORT)、
"db.name=" + postgres13TC.getDatabaseName()、
"db.username=" + postgres13TC.getUsername()、
"db.password=" + postgres13TC.getPassword()
);
}
}
最後にテストを実行すれば、ボンネットの中ですべてが開始される:
SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureTestDatabase(replace = NONE)
@ContextConfiguration(initializers = ContainerInit.class)
テストコンテナ
クラス DummyRepositoryTest
自動化
private DummyRepository dummyRepository;
テスト
void shouldReturnDummy() { 以下のようにします。
var byId = dummyRepository.getById(10L);
var expected = new Dummy();
expected.setId(10L);
assertThat(byId).completes().emitsCount(1).emits(expected);
}
}
また、Springに依存しないテストを実行したい場合は、コンテナを直接使用することもできる:
テストコンテナ
クラス SimpleDbTest
コンテナ
private static final Postgres13TC postgres13TC = Postgres13TC.getInstance();
テスト
void testConnection() {
assumeThat(postgres13TC.isRunning());
var connectionProps = new Properties();
connectionProps.put("user", postgres13TC.getUsername());
connectionProps.put("password", postgres13TC.getPassword());;
try (Connection connection = DriverManager.getConnection(postgres13TC.getJdbcUrl()、
connectionProps))
var resultSet = connection.prepareStatement("Select 1").executeQuery();
resultSet.next();
assertThat(resultSet.getInt(1)).isEqualTo(1);
} catch (SQLException sqlException) { {.
assertThat((Exception) sqlException).doesNotThrowAnyException();
}
}
}
Testcontainers are very easy-to-use tools that help us to create integration tests that use Docker containers. That gives us more flexibility and increases development speed. Proper setup of test configuration reduces the time needed to board new developers. They don’t need to set up all dependencies, just run the written tests with selected configuration files.
