Skip to content

Commit

Permalink
Upgrade to junit 5 #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Kulasek-Szwed committed Nov 18, 2019
1 parent 472b975 commit 4552a45
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 88 deletions.
13 changes: 7 additions & 6 deletions src/mokka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<!-- Excluded Junit 4 dependencies -->
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
Expand Down Expand Up @@ -260,15 +267,9 @@
</configuration>
</plugin>

<!-- Workaround for :
https://stackoverflow.com/questions/50661648/spring-boot-fails-to-run-maven-surefire-plugin-classnotfoundexception-org-apache/50661649#50661649
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>

</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package pl.hycom.mokka.core;

import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import pl.hycom.mokka.security.UserManager;

/**
Expand All @@ -18,12 +19,13 @@
* @author Piotr Kulasek-Szwed <[email protected]>
*/
@Slf4j
@Component
@Service
@RequiredArgsConstructor
public class InitialSetupCommandLineRunner implements CommandLineRunner {

@Autowired
private UserManager userManager;
private final UserManager userManager;

@Setter
@Value("${setup.initial.enabled}")
private boolean initialSetupEnabled;

Expand Down
6 changes: 3 additions & 3 deletions src/mokka/src/test/java/pl/hycom/mokka/AbstractTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package pl.hycom.mokka;

import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/**
* @author Mariusz Krysztofowicz ([email protected])
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureTestDatabase
@TestPropertySource(locations = "classpath:test.properties")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package pl.hycom.mokka.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import pl.hycom.mokka.security.UserManager;

import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* @author Piotr Kulasek-Szwed <[email protected]>
*/
@ExtendWith(MockitoExtension.class)
public class InitialSetupCommandLineRunnerTest {

@Mock
private UserManager userManager;

@InjectMocks
private InitialSetupCommandLineRunner initialSetupCommandLineRunner;

@Test
public void shouldSkipInitialConfig(){
// given
when(userManager.numberOfAdmins()).thenReturn(1);
initialSetupCommandLineRunner.setInitialSetupEnabled(true);

// when
try {
initialSetupCommandLineRunner.run("");
} catch (Exception e) {
Assertions.fail();
}

// then
verify(userManager, atLeastOnce()).numberOfAdmins();
}

@Test
public void shouldExecuteInitialConfig(){
// given
when(userManager.numberOfAdmins()).thenReturn(0);
initialSetupCommandLineRunner.setInitialSetupEnabled(true);

// when
try {
initialSetupCommandLineRunner.run("");
} catch (Exception e) {
Assertions.fail();
}

// then
verify(userManager, atLeastOnce()).createAdminUser();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.restassured.RestAssured;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
Expand All @@ -21,7 +21,7 @@ public class FileControllerTest extends AbstractTest {
@Value("${local.server.port}")
protected int serverPort;

@Before
@BeforeEach
public void setUp() {
RestAssured.port = serverPort;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package pl.hycom.mokka.service.file;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -12,42 +13,48 @@
*/
public class DefaultFileServiceTest {

DefaultFileService fileService = new DefaultFileService();
private DefaultFileService fileService;

@BeforeEach
public void init() {
fileService = new DefaultFileService();
}

@Test
public void testFetchFileSuccess() throws FileNotFoundException {
fileService.setSourceDirectory("src/test/resources/files/");
File file = fileService.fetchFile("samplefile.txt");
Assert.assertNotNull(file);
Assertions.assertNotNull(file);
}

@Test(expected = FileNotFoundException.class)
@Test
public void testFetchFileFileNotFoundException() throws FileNotFoundException {
fileService.setSourceDirectory("src/test/resources/files/");
fileService.fetchFile("samplefile1.txt");

Assertions.assertThrows(FileNotFoundException.class, () -> {
fileService.setSourceDirectory("src/test/resources/files/");
fileService.fetchFile("samplefile1.txt");
});
}

@Test(expected = IllegalArgumentException.class)
@Test
public void testFetchFileIllegalArgumentException() throws FileNotFoundException {
fileService.fetchFile(null);
Assertions.assertThrows(IllegalArgumentException.class, () -> fileService.fetchFile(null));

}

@Test
public void testFetchAllFileSuccess() {
fileService.setSourceDirectory("src/test/resources/files/");
List<String> files = fileService.fetchAllFiles();
Assert.assertNotNull(files);
Assert.assertEquals(1, files.size());
Assertions.assertNotNull(files);
Assertions.assertEquals(1, files.size());
}

@Test
public void testFetchAllFileWithoutFiles() {
fileService.setSourceDirectory("src/test/resources/files/dummy-directory/");
List<String> files = fileService.fetchAllFiles();
Assert.assertNotNull(files);
Assert.assertEquals(0, files.size());
Assertions.assertNotNull(files);
Assertions.assertEquals(0, files.size());
}

}
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
package pl.hycom.mokka.service.impl;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import pl.hycom.mokka.AbstractTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import pl.hycom.mokka.service.payment.DefaultPaymentStatusService;
import pl.hycom.mokka.service.payment.pojo.BlueMediaPayment;
import pl.hycom.mokka.service.payment.PaymentStatusService;

import javax.annotation.Resource;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;


/**
* @author Mariusz Krysztofowicz ([email protected])
*/
@Ignore
public class DefaultPaymentStatusServiceTest extends AbstractTest {
@Disabled
public class DefaultPaymentStatusServiceTest{

@Resource
private PaymentStatusService paymentStatusService;
private DefaultPaymentStatusService defaultPaymentStatusService;

@Rule
public WireMockRule wireMockRule = new WireMockRule(48080);
@Autowired
private MockRestServiceServer server;

@Before
@BeforeEach
public void init() {
wireMockRule.stubFor(post(urlPathMatching("/soap/webservice-http/payment")).willReturn(aResponse().withHeader("Content-Type", "text/plain")
.withBody("OK")
.withStatus(200)));
defaultPaymentStatusService = new DefaultPaymentStatusService();//new DefaultHashGenerator());
this.server.expect(requestTo("/soap/webservice-http/payment"))
.andRespond(withSuccess("OK", MediaType.TEXT_PLAIN));
}

@Test
public void TestSuccess() {
paymentStatusService.paymentStatusSuccessUpdate(createSampleBlueMediaPayment());
public void shouldSendSuccess() {
defaultPaymentStatusService.paymentStatusSuccessUpdate(createSampleBlueMediaPayment());
}

@Test
public void TestPending() {
paymentStatusService.paymentStatusPendingUpdate(createSampleBlueMediaPayment());
public void shouldSendPending() {
defaultPaymentStatusService.paymentStatusPendingUpdate(createSampleBlueMediaPayment());
}

@Test
public void TestFailure() {
paymentStatusService.paymentStatusFailureUpdate(createSampleBlueMediaPayment());
public void shouldSendFailure() {
defaultPaymentStatusService.paymentStatusFailureUpdate(createSampleBlueMediaPayment());
}

private BlueMediaPayment createSampleBlueMediaPayment() {
Expand Down
Loading

0 comments on commit 4552a45

Please sign in to comment.