Showing posts with label mock. Show all posts
Showing posts with label mock. Show all posts

AP Computer Science Mock Exam

Monday, April 25, 2016

For the past few years I have been involved with VASS , the Virginia branch of the national NMSI AP readiness grants. They work in increasing access to college by implementing rigorous AP programs in STEM areas in public schools.



The program has a specific methodology for increasing student success in AP classes. There are a ton of things they offer teachers, and Ill write about those separately.

The method they use for students could be implemented anywhere. This includes:


  1. Weekly hour long structured tutorials in AP classes. This is not just "does anyone have any questions?" but a structured session that focuses on elements of the curriculum that will most impact student success. I spend a lot of time in these sessions on tracing code and writing code on paper.
  2. External prep sessions that cover course topics more in depth. We do these online and each session lasts an hour. 
  3. The annual mock exam. Students sit for an AP style exam as practice. We use the released 2009 multiple choice questions (purchased) and the free response from the previous year. The results are used to steer the review for each individual student.


I have been grading Mock Exams for VASS  all this week. It is so nice to see my students arent the only one making goofy mistakes. After grading over 50 exams I have noticed a few patterns that might help you prepare your students for the free response section.


  1. Many of the questions are set up as methods:
    1. Do not rewrite the method header or make changes to the header! (this one boggles my mind)
    2. If they give you parameters - use them
    3. Do not re-declare the methods as local variables
    4. If the method returns an int, declare an int and return it
    5. If the method is void DO NOT RETURN A VALUE
  2. These questions are usually in a larger class. Look for how the data is stored - probably as an array, ArrayList or a 2-D array.
    1. Do not re-declare that object locally. Use the one from the class.
    2. Do not use [] with an ArrayList
    3. arrays and Strings use .length 0 ArrayList uses .size

  3. Look for patterns- especially if you are stuck. You can sometimes steal a few points even when you have no idea how to solve the problem.
    1. simple array? put in a for-loop
    2. does it ask a question? Youll need an if-statement

In the next week I am thinking of putting all of the free response question prompts from the past 5 years on a sheet of paper, handing out highlighters, and letting them look for these patterns.

I wouldnt do these all at once, but as you do practice coding on paper encourage them to think in terms of patterns. Even the best programmer can get stuck when having to code on paper. These tips should help them from making silly mistakes.


Read More..

Spring Java configuration to override and mock services using Mockito

Wednesday, March 5, 2014

In one of the previous posts entitled  Spring Java based configuration using @Configuration and @Bean we discussed how to use Java based configuration in Spring. We also looked at Mockito based examples and BDD using jBehave in this site.


Step 1: Lets say we have a AccountService class that we want to subject under test. While testing, we want to supply mocks for  BankAccountService and CashTransactionService. But use the real CalcEngine.


package com.myapp.services;

import static com.jpmorgan.wss.aes.calculation.strongersuper.engine.CalcEngineDroolsHelper.isNull;
//..

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


@Component("accountService")
public class AccountService
{


@Resource(name = "cashTransactionService")
private CashTransactionService cashTransactionService;


@Resource(name = "bankAccountService")
private BankAccountService bankAccountService;


@Resource(name = "calcEngine")
private CalcEngine calcEngine;


@Transactional(readOnly = true)
public List<balances> calcTransaction(int accountId)
throws IOException
{
//..............uses cashTransactionService, bankAccountService, and calcEngine
}

//...other methods

}


Step 2: The jBehave step class with Spring DI.

package com.myapp.bdd.steps;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.drools.runtime.StatelessKnowledgeSession;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.model.ExamplesTable;
import org.joda.time.DateTime;
import org.mockito.Mockito;
import org.springframework.stereotype.Component;

@Component
public class CashTransactionRulesStep
{

@Resource(name = "accountService")
private AccountService accountService;

@Resource(name = "cashTransactionService")
private CashTransactionService cashTransactionService;

@Resource(name = "bankAccountService")
private BankAccountService bankAccountService;

BankAccount bankAccount = new BankAccount();
List<CashTransaction> cashTransactionsList = Collections.EMPTY_LIST;
List<Transaction> result = null;

@Given("a bankAccountCd = $bankAccountCd and bankAccountNm = $bankAccountNm")
public void bankAccountDetails(String bankAccountCd, String bankAccountNm)
{
bankAccount.setAccountCd(bankAccountCd);
bankAccount.setAccountNm1(bankAccountNm);
}

@When("calcTransaction method is fired with portfoliocd = $portfolioCode")
public void calcTransaction(String portfolioCode)
{

try
{

Mockito.when(
cashTransactionService.findByAccountDt(Mockito.anyInt(), (Date) Mockito.anyObject(),
(Date) Mockito.anyObject())).thenReturn(cashTransactionsList);

Mockito.when(
bankAccountService.getBankAccount(Mockito.anyInt())).thenReturn(bankAccount);

result = transactionService.calcTransaction(1);

}
catch (Exception e)
{
throw new RuntimeException(e);
}

}

//......
}

Step 3: Now the Spring config class that overrides DI in AccountService with fully and partially mocked injection. The resource names need to be same to override, and nor defined under  @ComponentScan but defined with @Bean.

package com.myapp.bdd.stories;

import org.mockito.Mockito;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;

@Configuration("AccountingStoryConfig")
@ComponentScan(
basePackages =
{
"com.myapp.bdd",
"com.myapp.accounting",


},
useDefaultFilters = false,
includeFilters =
{
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ValidatorChain.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = TransactionValidator.class)
})
@Import(
{
StoryConfig.class //import other cobfigs
})
public class AccountingStoryConfig
{

@Bean(name = "accountService")
public TransactionService getAccountService()
{
return Mockito.spy(new AccountServiceImpl()); //partially mock
}

@Bean(name = "cashTransactionService")
public CashTransactionService getCashTransactionService()
{
return Mockito.mock(CashTransactionServiceImpl.class); //fully mock
}

@Bean(name = "bankAccountService")
public BankAccountService getBankAccountService()
{
return Mockito.mock(BankAccountService.class); //fully mock
}

}


Step 4: Finally, for completion sake, the jBehave story class that can run as jUnit test.

package com.myapp.bdd.stories;

import java.util.List;

import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.StoryFinder;
import org.springframework.batch.core.configuration.support.ClassPathXmlApplicationContextFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class AccountStory extends AllStories
{

public AccountStory()
{
super();
}

protected ApplicationContext context()
{
if (context == null)
{
try
{
ClassPathXmlApplicationContextFactory factory = new ClassPathXmlApplicationContextFactory(null);
factory.setApplicationContext(new AnnotationConfigApplicationContext(AccountStoryConfig.class));

context = factory.createApplicationContext();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
return context;
}

@Override
protected List<string> storyPaths()
{
return new StoryFinder().findPaths(
CodeLocations.codeLocationFromClass(getClass()), "**/account.story", "");
}

}



Read More..