I'm using allure2+testng+gradle and I can generate allure report similar with this https://github.com/sskorol/allure2-testng-report. The problem is that I can't see any ignored by TestNG tests. I.e. I have 2 tests:
#Test
#Story("Test 1")
public void test1() {
}
#Test(enabled = false)
#Story("Test 2")
public void test2() {
}
How to display in allure report that Test2 is ignored ?
resolved by adding allure-testng-adaptor
Related
I need to get trending articles from the community. I created a apex class for that by using ConnectApi.Knowledge.getTrendingArticles(communityId, maxResult).
I need to create a test class for that. I am using test class method provided by Salesforce for that. setTestGetTrendingArticles(communityId, maxResults, result) but I am getting this error "System.AssertException: Assertion Failed: No matching test result found for Knowledge.getTrendingArticles(String communityId, Integer maxResults). Before calling this, call Knowledge.setTestGetTrendingArticles(String communityId, Integer maxResults, ConnectApi.KnowledgeArticleVersionCollection result) to set the expected test result."
public without sharing class ConnectTopicCatalogController {
#AuraEnabled(cacheable=true)
public static List<ConnectApi.KnowledgeArticleVersion> getAllTrendingArticles(){
string commId = [Select Id from Network where Name = 'Customer Community v5'].Id;
ConnectApi.KnowledgeArticleVersionCollection mtCollection = ConnectApi.Knowledge.getTrendingArticles(commId, 12);
System.debug('getAllTrendingTopics '+JSON.serializePretty(mtCollection.items));
List<ConnectApi.KnowledgeArticleVersion> topicList = new List<ConnectApi.KnowledgeArticleVersion>();
for(ConnectApi.KnowledgeArticleVersion mtopic : mtCollection.items)
{
topicList.add(mtopic);
}
return topicList;
}
}
Test class that I am using for this
public class ConnectTopicCatalogControllerTest {
public static final string communityId = [Select Id from Network where Name = 'Customer Community v5'].Id;
#isTest
static void getTrendingArticles(){
ConnectApi.KnowledgeArticleVersionCollection knowledgeResult = new ConnectApi.KnowledgeArticleVersionCollection();
List<ConnectApi.KnowledgeArticleVersion> know = new List<ConnectApi.KnowledgeArticleVersion>();
know.add(new ConnectApi.KnowledgeArticleVersion());
know.add(new ConnectApi.KnowledgeArticleVersion());
system.debug('know '+know);
knowledgeResult.items = know;
// Set the test data
ConnectApi.Knowledge.setTestGetTrendingArticles(null, 12, knowledgeResult);
List<ConnectApi.KnowledgeArticleVersion> res = ConnectTopicCatalogController.getAllTrendingArticles();
// The method returns the test page, which we know has two items in it.
Test.startTest();
System.assertEquals(12, res.size());
Test.stopTest();
}
}
I need help to solve the test class
Thanks.
Your controller expects the articles to be inside the 'Customer Community v5' community, but you are passing the communityId parameter as null to the setTestGetTrendingArticles method.
Test class :
#isTest
public class PriceName_PriceList_test {
#istest static void PriceName_PriceListMethod(){
PriceName_PriceList priceObj = new PriceName_PriceList();
Product_Line_Item__c prl = New Product_Line_Item__c();
prl = [SELECT Product_Name__r.name, List_Price__c FROM Product_Line_Item__c];
prl.Product_Name__r.name = 'testproduct';
prl.List_Price__c = 123;
insert prl;
PriceObj.getdetails();
}
}
You didn't pass the Id to the class. So with a null the query will return 0. But it's interesting, I'd expect you to at least get coverage for line 6...
Change your code to this and run again
insert prl;
PriceObj.drId = prl.Id;
PriceObj.getdetails();
Or make the function accept an Id parameter and call PriceObj.getdetails(prl.Id), no idea what's your use case
Can #Epic have dynamic values in Allure report.
I need to pass my API name like #Epic(ApiName).
Private String ApiName = getApiName();
#Epic(ApiName)
you can use Allure.getLifecycle() api solved this problem.
ex:
#Test(dataProvider = "customer", dataProviderClass = GetUserData.class)
public void CommonMethod(UserData data) throws IOException, InterruptedException {
AllureLifecycle lifecycle = Allure.getLifecycle();
final Label label = new Label().setName("epic").setValue(data.getApi());
lifecycle.updateTestCase(testResult -> testResult.getLabels().add(label));
//testing code
}
I know it is not a good thing write a unit test for mapper or get set but, it is what it is, so I am stucked how to do unit test for mappers;
StudentGroupList below;
#Getter
#Setter
public class StudentGroupList {
private String studentId;
}
StudentGroupListRowMapper below;
public class StudentGroupListRowMapper implements RowMapper<StudentGroupList> {
#Override
public StudentGroupList mapRow(Resultset rs, int rowNum) throws SQLException {
StudentGroupList studentGroupList = new StudentGroupList();
studentGroupList.setStudentId(rs.getString("student_id"));
return studentGroupList;
}
}
I have tried below, but jococo coverage test did not coverage anything
public class TaskGroupListRowMapperTest {
private ResultSet resultSet;
private StudentGroupList studentGroupList;
#Before
public void setUp() {
resultSet = mock(ResultSet.class);
studentGroupList = mock(StudentGroupList.class);
}
#Test
public void testStudentGroupListMapper() throws SQLException {
when(resultSet.getString("student_id"))
.thenReturn(studentGroupList.getStudentID());
assertTrue(studentGroupList.getStudentId(), true);
}
}
İt says exception; thenReturn() may be missing.
Take it easy, we were all there in the past and try to understand what a unit test is supposed to do.
You don't unit test everything just for the sake of unit test coverage. When you have a framework callback like RowMapper, it is one of those cases. Your StudentGroupListRowMapper is very simple, so an integration test for your Dao will cover it. Anyway, you want to unit test so just think of it as a simple class and let's go through the steps.
You want to create the instance of the class you want to test and also provide mock dependencies for any services it calls. Luckily your StudentGroupListRowMapper does not call anything. Since the method you want to test is StudentGroupList mapRow(Resultset rs, int rowNum), you have to decide if you can provide a Resultset and a rowNum. Since Resultset is not something you create, your provide a mock for that
Resultset inputRs = mock(Resultset.class);
int rowNum = 1;
Now when your method executes, it is going call inputRs.getString("student_id") to get student id but it is mock and it doesn't know what to return so you have to tell your mock what to do when inputRs.getString("student_id") is called
when(inputRs.getString("student_id")).thenReturn("sutdent-id-1");
Now you know your expected StudentGroupList should be created with "sutdent-id-1" and should be returned from the method.
assertEquals(resultedStudentGroupList.getStudentId(),"sutdent-id-1");
Lets combine all of them together.
public class StudentGroupListRowMapperTest {
StudentGroupListRowMapper mapper = new StudentGroupListRowMapper();
#Test
public void testMapRow() {
Resultset inputRs = mock(Resultset.class);
int rowNum = 1;
when(inputRs.getString("student_id")).thenReturn("sutdent-id-1");
StudentGroupList result = mapper.mapRow(inputRs, rowNum);
assertEquals(result.getStudentId(), "sutdent-id-1");
}
}
I have 3 tests defined inside a class
#Test
public void test1(){
enter userID
}
#Test
public void test2(){
enter password
}
#Test
public void test3(){
click loginButton
}
But tests starts executing from test3 clicks loginButton first rather than in an order.
In TestNG the ordering of methods in the class file is unpredictable, so you need to either use dependencies[dependsOnMethods or priority] or include your methods explicitly in XML[preserver-order=true in your testng.xml].
#Test //( priority = 1 )
public void test1(){
enter userID
}
#Test(dependsOnMethods="test1") //( priority = 2 )
public void test2(){
enter password
}
#Test(dependsOnMethods="test2") //( priority = 3 )
public void test3(){
click loginButton
}