-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: Add Unit tests for the PreTranslateAction.java
class
#664
Conversation
Added more Unit tests to FileUtils class Improved code coverage from 70% to 95%
Added Unit tests for the ListLanguagesAction.java class
Codecov Report
@@ Coverage Diff @@
## main #664 +/- ##
============================================
+ Coverage 63.66% 64.01% +0.36%
- Complexity 1380 1385 +5
============================================
Files 210 209 -1
Lines 5648 5643 -5
Branches 851 849 -2
============================================
+ Hits 3595 3612 +17
+ Misses 1594 1570 -24
- Partials 459 461 +2 |
@Sweetdevil144 could you please specify which private classes you mean? I guess it should be possible to cover the other methods in this class by passing different values to the |
Oh Sorry. I mistakenly types classes. Updated the comment |
Sure. let me try that too. |
@Sweetdevil144 it looks like the |
Hey @andrii-bodnar . I tried adjusting the files as you recommeded. However I'm getting a lot of NullPointerExceptions at lines like -> action.act(out, properties, client); for test cases like @Test
void testPrepareFileIds_SomeSourcesDoNotExistInProjectPaths() {
Language english = new Language();
english.setId("en");
when(project.getProjectLanguages(false)).thenReturn(Collections.singletonList(english));
when(project.getDirectories()).thenReturn(new HashMap<>());
when(project.getBranches()).thenReturn(new HashMap<>());
when(project.getFileInfos()).thenReturn(Collections.emptyList());
action.act(out, properties, client);
verify(out, times(1)).println(anyString());
} |
Hey @Sweetdevil144, probably you need to mock the Crowdin client. Like in this test - |
Sorry @andrii-bodnar . This seems unbelievably 'Out' of my reach :- | |
I'm still encountring RuntimeError Exceptions which is demonstrated by the recent commit |
@Sweetdevil144 it says "Language 'en' doesn't exist in the project". It means that your project mock doesn't return this language. Probably, you'll need to update this mock: when(project.getProjectLanguages(false)).thenReturn(Collections.<Language>emptyList()); to return a list of languages instead of an empty list. |
Sorry @andrii-bodnar , I cant seem to get a hold of this. I would like to close this PR! |
@Sweetdevil144 I just looked closer at this and prepared an almost working example based on your code: package com.crowdin.cli.commands.actions;
import com.crowdin.cli.client.ProjectBuilder;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder;
import com.crowdin.cli.properties.PropertiesWithFiles;
import com.crowdin.cli.client.CrowdinProjectFull;
import com.crowdin.cli.utils.Utils;
import com.crowdin.client.translations.model.PreTranslationStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Collections;
import static org.mockito.Mockito.*;
class PreTranslateActionTest {
@Mock
private Outputter out;
@Mock
private ProjectClient client;
@Mock
private PreTranslationStatus preTranslationStatus;
private PreTranslateAction action;
private PropertiesWithFiles pb;
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
action = new PreTranslateAction(
Collections.singletonList("ua"), null, 1L, null, null, false, false, false, false, false, false, false, null
);
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("android.xml", "%two_letters_code%.xml")
.setBasePath(Utils.PATH_SEPARATOR);
pb = pbBuilder.build();
CrowdinProjectFull project = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
.addFile("android.xml", "android", 101L, null, null, "%two_letters_code%.xml")
.build();
when(client.downloadFullProject(null)).thenReturn(project);
when(client.startPreTranslation(any())).thenReturn(preTranslationStatus);
when(preTranslationStatus.getStatus()).thenReturn("finished");
}
@Test
void testAct_FailureWhenPreTranslationFails() {
when(preTranslationStatus.getStatus()).thenReturn("processing", "failed");
when(client.checkPreTranslation(anyString())).thenReturn(preTranslationStatus);
action.act(out, pb, client);
}
} Currently, this is failing with the Maybe we can create some temporary files locally during the test execution? These files should match the |
Fixes #659 .